Opened 12 years ago

Last modified 10 years ago

#16458 closed Bug

Equality operator on django.db.models.Model not commutative — at Initial Version

Reported by: freek.wiekmeijer@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: anssi.kaariainen@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Problem outline

In math is is a common assumption that the equality operator should be commutative, i.e. A==B would always correspond to B==A.
In django's Model class I found non-commutative behaviour.

Affected code


class Model(object):

def eq(self, other):

return isinstance(other, self.class) and self._get_pk_val() == other._get_pk_val()


Problem detail

This implementation of eq() will cause a different outcome between (A==B) and (B==A) in cases where one of the two compared objects is a derived class of the other.

assume this:
class MyModel(models.Model)
class MyDerivedModel(MyModel)
A = MyModel()
B = MyDerivedModel()

(A==B) # --> A.eq returns True if ( ==
(B==A) # --> B.eq returns False

Further analysis

I checked how derived models are created in the database (MySQL 5.0 backend). The table for MyDerivedModel does not contain its own primary key, but a reference to the PK in MyModel instead. This means that there can never be a collision between the primary key of MyModel and the primary key of MyDerivedModel.

So it is safe to compare PKs across base and derived classes, as is currently done.

Suggested solution

Make the type comparison between self and other symmetrical.

class Model(object):

def eq(self, other):

return (isinstance(other, self.class) or isinstance(self, other.class)) and \

(self._get_pk_val() == other._get_pk_val())

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top