Opened 5 years ago

Closed 21 months ago

Last modified 21 months ago

#16739 closed Bug (duplicate)

Model field clash not detected beyond parent

Reported by: Leo Shklovskii Owned by:
Component: Core (System checks) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When a local field in a class has the same name as a field in the superclass Django throws a FieldError when it spins up. However, it only checks the immediate parent, it doesn't go further up the tree so the following code doesn't trigger a FieldError (which it should):

class A(models.Model):
    foo = models.IntegerField()

class B(A):
    pass

class C(B):
    foo = models.IntegerField()

Attachments (1)

16739-test.diff (748 bytes) - added by Tim Graham 2 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 5 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

I confirm the bug. With this code in a models.py:

class GrandParent(models.Model):
    number = models.IntegerField()

class Parent(GrandParent):
    pass

class Child(Parent):
    number = models.IntegerField()

Expected result of ./manage.py validate:

FieldError: Local field 'number' in class 'Child' clashes with field of similar name from base class 'GrandParent'

Actual result: no error.

I tried to write a test case in invalid_models, but that didn't work because the name clash creates an exception, not a validation error.

comment:2 Changed 4 years ago by swcisel

Owner: changed from nobody to swcisel
Status: newassigned

comment:3 Changed 4 years ago by Anssi Kääriäinen

Type: UncategorizedBug

comment:4 Changed 2 years ago by Tim Graham

Component: Database layer (models, ORM)Core (System checks)
Owner: swcisel deleted
Status: assignednew
Summary: field clash not detected beyond parentModel field clash not detected beyond parent

Confirmed this is still an issue with the attached test for Django's test suite.

Changed 2 years ago by Tim Graham

Attachment: 16739-test.diff added

comment:5 Changed 21 months ago by Karl

I think this ticket can be marked as fixed or duplicate now, fixed here #24249.

comment:6 Changed 21 months ago by Claude Paroz

Resolution: duplicate
Status: newclosed

Right, thanks for checking (Tim's test is now passing well).

comment:7 Changed 21 months ago by Leo Shklovskii

Thanks guys! Great to have this cleaned up!

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