﻿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
34125	Limiting QuerySet crashes on union() with a single non-empty query	Stefan Hammer	Simon Charette	"We have an union-query, that works up until django 4.1.2, but stopped working with the latest dev-version of django.
Basically, one QuerySet is non-empty, and the other one is empty, simplified:
{{{
q1 = PageLogEntry.objects.all().order_by()
q2 = q1.union(PageLogEntry.objects.none())
q2.order_by()[:1]
}}}

With PostgreSQL this leads to the following SQL (I've reduced the SQL to one field only -> ""...""):
{{{
(SELECT ""wagtailcore_pagelogentry"".""id"", ... FROM ""wagtailcore_pagelogentry"" LIMIT 1) LIMIT 1;
}}}

which results in this error:
{{{
psycopg2.errors.SyntaxError: multiple LIMIT clauses not allowed
LINE 1: ..."".""page_id"" FROM ""wagtailcore_pagelogentry"" LIMIT 1) LIMIT 1
}}}

With django 4.1.2 the following valid SQL is generated:

{{{
(SELECT ""wagtailcore_pagelogentry"".""id"", ... FROM ""wagtailcore_pagelogentry"") LIMIT 1;
}}}

With sqlite no errors are thrown in the dev-version.

Note: In our actual queries, we use ordering (we remove ordering in the inner queries with `order_by()` and apply an ordering to the resulting union-queryset), but that does not change anything, since the limit seems to be the issue, so I've removed ordering to minimize the SQL.

I've found a similar, fixed bug #32116, but in that case order_by had an issue."	Bug	closed	Database layer (models, ORM)	dev	Release blocker	fixed		bcail Simon Charette	Accepted	1	0	0	0	0	0
