Opened 4 years ago

Closed 4 years ago

#19102 closed Bug (fixed)

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

Reported by: akaariai Owned by: akaariai
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 4 years ago by pressureman

  • Cc django@… added

comment:2 Changed 4 years ago by akaariai

  • Owner changed from nobody to akaariai

comment:3 Changed 4 years ago by akaariai

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

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

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

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