Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-validate uniqueness. #11

Open
tomchristie opened this issue Mar 18, 2019 · 4 comments
Open

Pre-validate uniqueness. #11

tomchristie opened this issue Mar 18, 2019 · 4 comments

Comments

@tomchristie
Copy link
Member

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.

@florimondmanca
Copy link
Member

florimondmanca commented Mar 25, 2019

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?

@tomchristie
Copy link
Member Author

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.

@tomchristie
Copy link
Member Author

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.

@florimondmanca
Copy link
Member

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants