﻿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
16159	ModelForm does not catch unique error with model inheritance of more than 2 levels	mturtle	nobody	"Ticket #12881 describes the main problem, which was partially fixed. However, when you have a larger inheritance hierarchy, the form doesn't catch it in the validation, and instead a unique constraint error is raised by postgres.

The culprit:

{{{
class A(Model):
    username = CharField(max_length=255, unique=True)

class B(A):
    pass

class C(B):
    pass
}}}

(Note C extends B, and B extends A.)

In Django 1.2.5 in django/db/models/base.py, around line 762, I see the following:

{{{
        fields_with_class = [(self.__class__, self._meta.local_fields)]
        for parent_class in self._meta.parents.keys():
            fields_with_class.append((parent_class, parent_class._meta.local_fields))
}}}

When I change `self._meta.parents.keys()` to include the entire chain of superclasses, the problem is fixed. I don't know the best-practice way of calculating the chain of superclasses up to but not including Model, but my dirty hack method of demonstrating this is by changing `self._meta.parents.keys()` to `self._meta.get_base_chain(type(self).__bases__[0].__bases__[0])`"	Bug	closed	Database layer (models, ORM)	1.2	Normal	duplicate	unique constraint, ModelForm, inheritance		Unreviewed	0	0	0	0	0	0
