[Django] #24951: Error deleting from table with Foreign Key being Primary Key at the same point

6 views
Skip to first unread message

Django

unread,
Jun 8, 2015, 9:52:45 AM6/8/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
----------------------------------------------+--------------------
Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24951>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 10, 2015, 9:15:04 AM6/10/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------

Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_docs: => 0
* needs_better_patch: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:1>

Django

unread,
Jun 10, 2015, 9:15:48 AM6/10/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------

Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* Attachment "24951-test.diff" added.

Django

unread,
Aug 5, 2015, 11:02:53 AM8/5/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------

Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by guettli):

* cc: hv@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:2>

Django

unread,
Aug 5, 2015, 11:05:36 AM8/5/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------

Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by guettli):

Is there are known work around until this is fixed?

--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:3>

Django

unread,
Aug 5, 2015, 1:56:56 PM8/5/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------

Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* severity: Normal => Release blocker


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:4>

Django

unread,
Aug 5, 2015, 5:00:32 PM8/5/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------

Reporter: grumbler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/5109 Removing the assertion] does
work, but I guess it probably isn't the correct fix. Any advice from ORM
experts?

--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:5>

Django

unread,
Aug 20, 2015, 8:30:39 AM8/20/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------
Reporter: grumbler | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"333cbdcd2de4546e33ad50ebd8b67e1a1e87aeec" 333cbdc]:
{{{
#!CommitTicketReference repository=""
revision="333cbdcd2de4546e33ad50ebd8b67e1a1e87aeec"
Fixed #24951 -- Fixed AssertionError in delete queries involving a
foreign/primary key.

Thanks Anssi Kääriäinen for help.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:6>

Django

unread,
Aug 20, 2015, 8:53:23 AM8/20/15
to django-...@googlegroups.com
#24951: Error deleting from table with Foreign Key being Primary Key at the same
point
-------------------------------------+-------------------------------------
Reporter: grumbler | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"ffe755e990fa7e680960c7a413d387a79f194204" ffe755e9]:
{{{
#!CommitTicketReference repository=""
revision="ffe755e990fa7e680960c7a413d387a79f194204"
[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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24951#comment:7>

Reply all
Reply to author
Forward
0 new messages