Code

Opened 4 years ago

Closed 3 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: UI/UX:

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))

Attachments (0)

Change History (3)

comment:1 Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 4 years ago by thejaswi_puthraya

  • Component changed from Uncategorized to Database layer (models, ORM)

comment:3 Changed 3 years ago by russellm

  • Resolution set to invalid
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.