-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Fixed #11964 -- Added support for check constraints in model Meta. #7615
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
Conversation
django/db/models/constraints.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work because I don't have access to a suitable compiler
. I will also need to add support for as_sql
to Q
objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From #8056, it looks like I can do:
compiler = connection.ops.compiler('SQLCompiler')(None, connection, 'default')
f7f256c
to
89b2092
Compare
django/db/models/constraints.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gets me further. Unfortunately, I appear to need a more complete query
instead of None
, so Col.as_sql
can be evaluated.
tests/migrations/test_operations.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long term, I want to be able to use Q(pink__gt=2)
here, but for now using this avoids the need to implement Q.as_sql
.
tests/migrations/test_state.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't really thought whether this is the best way to fix these tests, but it is sufficient for now.
fc2f707
to
835f04e
Compare
I'm not sure where is best to put MySQL error handling. It supports the syntax, but doesn't actually do anything with it, so I think we should fail loud somewhere. Perhaps in a check? |
I'm also not sure how best to handle SQLite. It appears to support the feature, but I get a syntax error from SQLite with the syntax that works on postgres. I think this is because that uses Django does set |
835f04e
to
d928c31
Compare
@jarshwah @MarkusH I'd appreciate your feedback on this. There's definitely some work left to do - docs, MySQL, SQLite, naming consistency, any edge-cases I've missed - but I think the core is mostly there. I'd also like to add a test to check an |
50f4ae0
to
5e97e81
Compare
docs/ref/models/options.txt
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be 2.0
00ee601
to
505beef
Compare
44e6a30
to
76217f8
Compare
This now just needs to do something sensible for MySQL and the rest is cleanup and edge-cases. |
django/db/models/sql/query.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be possible to simplify this further. I'm also not 100% certain I've thought of every edge-case, so I may have accidentally deleted something important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels odd why so much code needs to be duplicated. Have you considered letting Query
subclass this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't considered that. My main focus has been trying to get something that works well for this use case. Refactoring to allow code re-use is a secondary concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lot of stuff to duplicate, especially as you note below that you don't understand everything. I'm concerned that future expression refactors/features will be missed in constraints and things will asplode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - that's a good point. I'll take another pass and see if I can minimise the amount of duplicated code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.
https://code.djangoproject.com/ticket/11964