#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.

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.ca8650f818dd103b0ebbef3487ecbd02%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to