Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#24951 closed Bug (fixed)

Error deleting from table with Foreign Key being Primary Key at the same point

Reported by: Serge Travin Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Release blocker Keywords:
Cc: hv@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Problem occurs, when trying to issue delete on queryset for a table like this:

class Derivative(models.Model):
    spot = models.OneToOneField(Spot, primary_key=True)
    flag = models.BooleanField(default=True)

Derivative.objects.filter(spot__name__icontains='name').delete()

Result is:
AssertionError: Can only delete from one table at a time.

However, if we have simple table like this:

class SimpleDerivative(models.Model):
    spot = models.OneToOneField(Spot)
    flag = models.BooleanField(default=True)

than there is no problem. Note, there is no primary_key for spot here.

Problem does not exist for Django 1.6 (I didn't check for previous versions)
And persits in 1.7 and 1.8

There is changed code between versions in:
django/db/models/sql/subqueries.py - DeleteQuery.delete_qs

right before the last line self.tables contains two tables, which leads to assertion error.

I provided sample project to reproduce issue - https://github.com/grumbler/django_delete_issue
Simply run the tests with the appropriate Django version installed.

Attachments (1)

24951-test.diff (1.2 KB) - added by Tim Graham 5 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 5 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

Bisected to 20bab2cf9d02a5c6477d8aac066a635986e0d3f3. Attaching a regression test for Django's test suite.

Changed 5 years ago by Tim Graham

Attachment: 24951-test.diff added

comment:2 Changed 5 years ago by Thomas Güttler

Cc: hv@… added

comment:3 Changed 5 years ago by Thomas Güttler

Is there are known work around until this is fixed?

comment:4 Changed 5 years ago by Tim Graham

Severity: NormalRelease blocker

Not sure, but probably should be a release blocker since it's a regression.

comment:5 Changed 5 years ago by Tim Graham

Has patch: set
Patch needs improvement: set

Removing the assertion does work, but I guess it probably isn't the correct fix. Any advice from ORM experts?

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 333cbdc:

Fixed #24951 -- Fixed AssertionError in delete queries involving a foreign/primary key.

Thanks Anssi Kääriäinen for help.

comment:7 Changed 5 years ago by Tim Graham <timograham@…>

In ffe755e9:

[1.8.x] Fixed #24951 -- Fixed AssertionError in delete queries involving a foreign/primary key.

Thanks Anssi Kääriäinen for help.

Backport of 333cbdcd2de4546e33ad50ebd8b67e1a1e87aeec from master

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