Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#31750 closed Bug (fixed)

Abstract model field should not be equal across models

Reported by: Ryan Hiebert Owned by: Ryan Hiebert
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Ryan Hiebert Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the following models:

class A(models.Model):
    class Meta:
        abstract = True
    myfield = IntegerField()

class B(A):
    pass

class C(A):
    pass

If I pull the fields of B and C into a shared set, one will be de-duplicated away, because they compare as equal. I found this surprising, though in practice using a list was sufficient for my need. The root of the issue is that they compare equal, as fields only consider self.creation_counter when comparing for equality.

len({B._meta.get_field('myfield'), C._meta.get_field('myfield')}) == 1
B._meta.get_field('myfield') == C._meta.get_field('myfield')

We should adjust __eq__ so that if the field.model is different, they will compare unequal. Similarly, it is probably wise to adjust __hash__ and __lt__ to match.

When adjusting __lt__, it may be wise to order first by self.creation_counter so that cases not affected by this equality collision won't be re-ordered. In my experimental branch, there was one test that broke if I ordered them by model first.

I brought this up on IRC django-dev to check my intuitions, and those conversing with me there seemed to agree that the current behavior is not intuitive.

Change History (6)

comment:1 by Carlton Gibson, 4 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

OK, happy to look at a patch — assuming nothing breaks...

It'd be a change in behaviour, so would need calling out in the release notes.

comment:2 by Ryan Hiebert, 4 years ago

Cc: Ryan Hiebert added
Has patch: set
Owner: changed from nobody to Ryan Hiebert
Status: newassigned

PR

Should this get backported? It feels like an (admittedly esoteric) bug fix to me. It affects ordering, though I've minimized that effect to only places where the ordering is currently undefined, as they are currently considered equal.

comment:3 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 502e75f9:

Fixed #31750 -- Made models.Field equality compare models for inherited fields.

comment:4 by Nicolas Delaby, 3 years ago

Hi, I wonder if the submitted fix didn't introduce a subtle regression.
Given the example posted in the description, how would you evaluate the following statements ?

before https://github.com/django/django/commit/502e75f9ed5476ffe8229109acf0c23999d4b533

assert B._meta.get_field('myfield') == C._meta.get_field('myfield')
assert B._meta.get_field('myfield') == A._meta.get_field('myfield')
assert C._meta.get_field('myfield') == A._meta.get_field('myfield')

after https://github.com/django/django/commit/502e75f9ed5476ffe8229109acf0c23999d4b533

assert B._meta.get_field('myfield') != C._meta.get_field('myfield')
assert B._meta.get_field('myfield') != A._meta.get_field('myfield')
assert C._meta.get_field('myfield') != A._meta.get_field('myfield')

What if it shouldn't be this ? Since B (or C) doesn't override myfield, I would expect the subclass to evaluate equality as if the field was defined on itself (given the parent model is abstract)
maybe?

assert B._meta.get_field('myfield') != C._meta.get_field('myfield')
assert B._meta.get_field('myfield') == A._meta.get_field('myfield')
assert C._meta.get_field('myfield') == A._meta.get_field('myfield')

comment:5 by Mariusz Felisiak, 3 years ago

Hi, I wonder if the submitted fix didn't introduce a subtle regression.

This is an expected change IMO. Model fields don't make much sense without models (even abstract) so we should distinguish between fields in abstract models and the same fields inherited from them.

comment:6 by Nicolas Delaby, 3 years ago

Thanks for clarifying Mariusz.

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