﻿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
34091	Invalid SQL: FROM clauses can be omitted when QuerySet is accessed from multiple threads	Marti Raudsepp	nobody	"I am experiencing very rare cases, where Django generates invalid SQL for `select_related()` tables. This occurs in a place where multiple threads are trying to execute the same QuerySet at the same time. (The access from multiple threads was unintentional, but I believe this deserves to be fixed in Django anyway)

When this occurs, tables/joins in the FROM clause can be missing entirely, but they are still referenced in the SELECT clause, and the query fails, for example `sqlite3.OperationalError: no such column: app_fk18.id` or `ORA-00904: ""xxx"".""xxx"": invalid identifier`

I could not reproduce this with vanilla Django, but after patching Django to add `time.sleep(0)` in `SQLCompiler.compile()`, I can reliably reproduce this.

The reproducer and longer explanation is here: https://github.com/intgr/bug-reports/tree/main/django-query-race-condition

I suspect some mutation of the `Query` object is going on during SQL compilation, but by efforts to narrow down the mutation have been unsuccessful so far.
"	Bug	closed	Database layer (models, ORM)	4.1	Normal	wontfix	race-condition, orm, threading, SQLCompiler		Unreviewed	0	0	0	0	0	0
