#32433: Delete distinct produces an unexpected and potentially harmful SQL
-------------------------------------+-------------------------------------
     Reporter:  egism                |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  orm, delete,         |             Triage Stage:  Accepted
  distinct                           |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * keywords:  orm, delete, distinct, postgresql => orm, delete, distinct
 * type:  Uncategorized => Bug
 * stage:  Unreviewed => Accepted


Comment:

 I think we should start by failing loudly when `distinct().delete()` is
 used as adding support for it would require a subquery pushdown (`DELETE
 FROM table WHERE field IN (SELECT DISTINCT field FROM table`) which would
 require a non negligible amount of work and additional complexity for the
 rare cases where this is useful and a manual `__in` subquery lookup can be
 used to achieve the same results e.g.

 {{{#!python
 Comment.objects.filter(
     post_id__in=Comment.objects.order_by('post_id',
 'created_at').distinct('post_id').values('post_id')
 ).delete()
 }}}

 **egism** if you want to work on a patch you should raise a `TypeError` in
 `QuerySet.delete` if `self.query.distinct or self.query.distinct_fields`.
 A regression test can then be added to `delete_regress/tests`
 
([https://github.com/django/django/blob/5fcfe5361e5b8c9738b1ee4c1e9a6f293a7dda40/tests/delete_regress/tests.py
 source]).

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.05083ef1e0fe14fbd798d7ef7dded111%40djangoproject.com.

Reply via email to