Visitar URL original
Pre-validate uniqueness. · Issue #11 · encode/orm · GitHub
Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Pre-validate uniqueness. #11

Description

@lovelydinosaur

Eg. If username is a unique field, then User.objects.create(username=...) should ideally:

  • Run inside a transaction.
  • Run a query to check no existing username already exists. Raise an exception otherwise.
  • Perform the insert.

This is far more helpful than raising an integrity error.

Activity

  1. florimondmanca commented on Mar 25, 2019

    @florimondmanca
    Contributor

    I'm interested in working on this!

    Which type of exception should be raised? A new UniqueFieldExists exception? A typesystem.ValidationError?

    Also, could you explain why the transaction is required? 😄 Can't it be enough to execute the various validation queries and then perform the insert?

  2. lovelydinosaur commented on Mar 26, 2019

    @lovelydinosaur
    MemberAuthor

    A typesystem.ValidationError yup. At least for now.

    Also, could you explain why the transaction is required?

    Let's not worry about that at the moment. But yeah, ideally you'd want that, otherwise there's a race condition between "We've checked uniqueness" and "we've performed the insert". If you get caught by that race, then you can end up having an DB error raised, rather than a nice validation error.

  3. lovelydinosaur commented on Mar 26, 2019

    @lovelydinosaur
    MemberAuthor

    I mean Django doesn't cover it I think, nor REST framework, so it's not really a big deal, and the integrity of the data is still safe, so it's really not a big deal in practice.

  4. florimondmanca commented on Mar 26, 2019

    @florimondmanca
    Contributor

    Yes, I see. I also suppose we're safe since the data integrity is ensured by the database anyway (at least for MySQL and PostgreSQL).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions