Opened 5 years ago

Last modified 4 years ago

#31530 closed Cleanup/optimization

Check that CheckConstraint.check and UniqueConstraint.condition don't span joins. — at Version 1

Reported by: Simon Charette Owned by: nobody
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 Mariusz Felisiak)

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 (1)

comment:1 by Mariusz Felisiak, 5 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted
Note: See TracTickets for help on using tickets.
Back to Top