Opened 14 years ago

Closed 13 years ago

#14067 closed (invalid)

_perform_unique_checks, _perform_date_checks broken for subclasses

Reported by: minijb Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

fields marked unique on base classes are validated wrong on subclasses.

The broken part is in django/db/models/base.py:

            qs = model_class._default_manager.filter(**lookup_kwargs)

            # Exclude the current object from the query if we are editing an
            # instance (as opposed to creating a new one)
            if not getattr(self, '_adding', False) and self.pk is not None:
                qs = qs.exclude(pk=self.pk)

Specifically, the exclude statement filtering on model_class.pk=self.pk does not consider that self (an instance of the subclass) has a different pk to model_class (the base class). This results in the wrong pk being used to exclude on.

This could instead filter on self's value for model_classes pk like so:

            qs = model_class._default_manager.filter(**lookup_kwargs)

            # Exclude the current object from the query if we are editing an
            # instance (as opposed to creating a new one)
            if not getattr(self, '_adding', False) and self.pk is not None:
                qs = qs.exclude(pk=getattr(self, model_class._meta.pk.name))

Change History (3)

comment:1 by Karen Tracey, 14 years ago

A failing test case would help here. Seems to me you need to have multiple inheritance to get this to fail, or am I missing some way of having the pks of parent and child classes differ with single inheritance? I'm sure I did not consider multiple inheritance when fixing #12881 (I don't think I realized it was supported then) and I think I can see how you could get a wrong result from this code in the multi inheritance case, but I'd like to understand if there is also a problem in the single inheritance case.

comment:2 by Thejaswi Puthraya, 14 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:3 by Russell Keith-Magee, 13 years ago

Resolution: invalid
Status: newclosed

Closing invalid because the reporter hasn't responded to a request for a test case. Please reopen if you can provide such a test case.

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