[Django] #24542: Inheriting from multiple abstract model base classes with the same field leads to invalid SQL and db-level exception

12 views
Skip to first unread message

Django

unread,
Mar 26, 2015, 2:51:15 PM3/26/15
to django-...@googlegroups.com
#24542: Inheriting from multiple abstract model base classes with the same field
leads to invalid SQL and db-level exception
-------------------------------------+-------------------------------------
Reporter: Ghopper21 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Keywords: multiple model
Severity: Normal | inheritance sql fielderror
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Given the following model classes:

{{{
class Daddy(models.Model):

class Meta:
abstract = True

foobar = models.CharField(default="", max_length=20)


class Mommy(models.Model):

class Meta:
abstract = True

foobar = models.CharField(default="", max_length=20)


class Kid(Daddy, Mommy):
pass

}}}

Django will validate this. However, the foobar field will show up twice in
Kid._meta.local_fields, and if you try to save a Kid model instance,
Django will generate SQL code that references foobar twice, leading to a
hard-to-debug database level exception.

I suggest expected behavior to be either (1) throw a FieldError on
validation (consistent with what Django does if a child model tries to
"hide" a parent's field) or (2) ignore subsequent model fields from
abstract model base classes (per MRO-order) with the same name as existing
fields.

The real-world context that led to this bug discovery: in refactoring a
large existing project, I subclassed models.Model and had all project
models inherit from this subclass, which added a new field (let's call it
foobar). Some models had multiple abstract base classes that subclassed
from models.Model, in a typical mixin pattern. Django was validating and
then creating foobar twice for such models, leading to the database error
due to invalid SQL.

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

Django

unread,
Mar 26, 2015, 7:47:15 PM3/26/15
to django-...@googlegroups.com
#24542: Inheriting from multiple abstract model base classes with the same field
leads to invalid SQL and db-level exception
-------------------------------------+-------------------------------------
Reporter: Ghopper21 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.6
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: multiple model | Triage Stage:
inheritance sql fielderror | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* needs_docs: => 0
* resolution: => fixed
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

It's been fixed in Django 1.7 with the following validation error:
polls.Kid.foobar: (models.E006) The field 'foobar' clashes with the field
'foobar' from model 'polls.kid'.

--
Ticket URL: <https://code.djangoproject.com/ticket/24542#comment:1>

Django

unread,
Mar 29, 2015, 5:32:45 AM3/29/15
to django-...@googlegroups.com
#24542: Inheriting from multiple abstract model base classes with the same field
leads to invalid SQL and db-level exception
-------------------------------------+-------------------------------------
Reporter: Ghopper21 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.6
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: multiple model | Triage Stage:
inheritance sql fielderror | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Ghopper21):

Thanks Tim for the fast turnaround on this!

--
Ticket URL: <https://code.djangoproject.com/ticket/24542#comment:2>

Reply all
Reply to author
Forward
0 new messages