#31530 closed Cleanup/optimization (fixed)
Check that CheckConstraint.check and UniqueConstraint.condition don't span joins.
| Reported by: | Simon Charette | Owned by: | Hasan Ramezani |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Similar to #31410 but for check and condition.
Not everyone is familiar with the fact database level constraint cannot span across tables and might be tempted to do
class Person(models.Model): age = models.PositiveSmallIntegerField() parent = models.ForeignKey(self) class Meta: constraints = { CheckConstraint( name='age_lt_parent', check=Q(age__lt=parent__age) ), }
Which we'll happily create migrations for but we'll then crash because we prevent JOINs when resolving check.
Change History (10)
comment:1 by , 5 years ago
| Description: | modified (diff) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:3 by , 5 years ago
| Patch needs improvement: | set |
|---|
Is it even possible to handle all cases with Q objects?
As mentioned in this comment, custom expressions might be impossible to introspect.
Maybe we can rely on something like get_source_expressions to get all included fields...
comment:4 by , 5 years ago
Here is my comment on PR:
Yes, this doesn't work for Lower('parent__name'). I can change the patch to cover this case as well but I don't know exactly which kind of expression should I cover? and with more cases, the code is going to be complicated.
@felixxm Do you have any idea?
comment:5 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:6 by , 5 years ago
| Patch needs improvement: | set |
|---|---|
| Version: | 3.0 → master |
comment:7 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:8 by , 5 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
PR