﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37507	select_related removed from deletion queries	Julie Rymer		"Hi, following this discussion (https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857), there might be an issue with the preprocessing done on delete queries.


== Issue description

Inside django/db/models/query.py:

{{{
# django/db/models/query.py

class QuerySet(AltersData):
...
    def delete(self):
        """"""Delete the records in the current QuerySet.""""""
         ...
        # Disable non-supported fields.
        del_query.query.select_for_update = False
        del_query.query.select_related = False
        del_query.query.clear_ordering(force=True)
}}}

We can see that any `select_for_update`, `select_related` and ordering are removed from the query before the delete gets executed. It was probably added for performance's sake.
While it makes sense to remove ordering and `select_for_update`, clearing the `select_related` has side effects: if you're accessing the related model inside `pre_delete` or `post_delete` signals, you are doing N+1 queries:


{{{
MyModel.objects.select_related(""my_foreign_key"").filter(...).delete()
...
@receiver(post_delete, sender=MyModel)
def my_signal(sender, instance, **kwargs):
    # not prefetched, so does a db look up for every instance
    if instance.my_foreign_key:   
     ...
}}}

This might go unnoticed, unless you're using the new shiny `FETCH_RAISE` fetch mode option to raise an error instead of triggering a new query:

{{{
MyModel.objects.fetch_mode(models.FETCH_RAISE).select_related(""my_foreign_key"").filter(...).delete()
...
@receiver(post_delete, sender=MyModel)
def my_signal(sender, instance, **kwargs):
    # /!\ will raise with FieldFetchBlocked
    if instance.my_foreign_key:   
     ...
}}}

Considering `Queryset.delete()` will fetch and iterate on the result of the query, I don't think `select_related` should be cleared from delete queries.
Except maybe if `Collector._has_signal_listeners(model)` is False?


== Workarounds

Easy workarounds:
- Use `prefetch_related` instead of `select_related` as this doesn't get cleared **(preferred solution, tested)**
- Call `.fetch_mode(FETCH_PEERS)` before `.delete()` so the first access to the related field will trigger a full queryset and prevent a N+1 issue. (untested)
More convoluted workarounds:
- replace signal handler with a normal function that handles bulk deletion (untested but will obviously work, you lose the convenience of signals though)
- keep `select_related`, evaluate the queryset yourself and pass the list to `Collector.collect() / collector.delete()` so the cached related objects make it to the signal handlers (untested)"	Bug	new	Database layer (models, ORM)	6.1	Normal				Unreviewed	0	0	0	0	0	0
