#34964: Reversing the order of Q objects in a CheckConstraint generates a 
migration
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  Jacob
         Type:                       |  Walls
  Cleanup/optimization               |                   Status:  assigned
    Component:  Migrations           |                  Version:  dev
     Severity:  Normal               |               Resolution:
     Keywords:  noop                 |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 The creation of new migrations could be avoided entirely by performing the
 sorting only during `__eq__`

 {{{#!diff
 diff --git a/django/db/models/constraints.py
 b/django/db/models/constraints.py
 index 56d547e6b0..e221bebd8b 100644
 --- a/django/db/models/constraints.py
 +++ b/django/db/models/constraints.py
 @@ -152,14 +152,22 @@ def __repr__(self):
          )

      def __eq__(self, other):
 -        if isinstance(other, CheckConstraint):
 -            return (
 -                self.name == other.name
 -                and self.check == other.check
 -                and self.violation_error_code ==
 other.violation_error_code
 -                and self.violation_error_message ==
 other.violation_error_message
 -            )
 -        return super().__eq__(other)
 +        if not isinstance(other, CheckConstraint):
 +            return super().__eq__(other)
 +        # Relax check equality to equivalence comparisons to allow
 +        # re-ordering of components.
 +        if isinstance(self.check, Q):
 +            if not isinstance(other.check, Q) or not
 self.check.equivalent_to(
 +                other.check
 +            ):
 +                return False
 +        elif self.check != other.check:
 +            return False
 +        return (
 +            self.name == other.name
 +            and self.violation_error_code == other.violation_error_code
 +            and self.violation_error_message ==
 other.violation_error_message
 +        )

      def deconstruct(self):
          path, args, kwargs = super().deconstruct()
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34964#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018bc47bee52-04705b86-8aca-44c7-9043-e481b866e539-000000%40eu-central-1.amazonses.com.

Reply via email to