Opened 6 years ago

Closed 6 years ago

#19102 closed Bug (fixed)

Deletion using subqueries doesn't work for aggregates, extra or select_related

Reported by: Anssi Kääriäinen Owned by: Anssi Kääriäinen
Component: Database layer (models, ORM) Version: master
Severity: Release blocker Keywords:
Cc: django@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Aggregates, extra and select_related add columns to the subselect. However, the subselect must contain only one column.

Example case (modified from #18676's example):

class OrgUnit(models.Model):
    name = models.CharField(max_length=64, unique=True)
class Login(models.Model):
    description = models.CharField(max_length=32)
    orgunit = models.ForeignKey(OrgUnit)


The above fails with "too many columns in subquery" for various databases.

I have a work-in-progress patch for this at:

This is a regression caused by #18676, so marking as release blocker.

Change History (4)

comment:1 Changed 6 years ago by Daniel Swarbrick

Cc: django@… added

comment:2 Changed 6 years ago by Anssi Kääriäinen

Owner: changed from nobody to Anssi Kääriäinen

comment:3 Changed 6 years ago by Anssi Kääriäinen

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:4 Changed 6 years ago by Anssi Kääriäinen <akaariai@…>

Resolution: fixed
Status: newclosed

In f64a5ef404cb6fd28e008a01039a3beea2fa8e1b:

Fixed #19102 -- Fixed fast-path delete for modified SELECT clause cases

There was a bug introduced in #18676 which caused fast-path deletes
implemented as "DELETE WHERE pk IN <subquery>" to fail if the SELECT
clause contained additional stuff (for example extra() and annotate()).

Thanks to Trac alias pressureman for spotting this regression.

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