﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24542	Inheriting from multiple abstract model base classes with the same field leads to invalid SQL and db-level exception	Ien Cheng	nobody	"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.

"	Bug	closed	Database layer (models, ORM)	1.6	Normal	fixed	multiple model inheritance sql fielderror		Unreviewed	0	0	0	0	0	0
