I am trying to union 2 querysets having only certain columns, but facing error. For example, have given below the example with only 1 column.
Didn’t even use the Order_by clause, still Django shell environment throws error ‘django.db.utils.DatabaseError: ORDER BY not allowed in subqueries of compound statements.’
What is the exact syntax for union of 2 or more querysets? - Thank you!
**********************************************************************
** Visual Studio 2022 Developer PowerShell v17.2.3
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
PS D:\IT\Projects\Django\DevApp> python manage.py shell
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from Employee.models import *
>>> Dlists = DList.objects.values_list('List_ID')
>>> HLists = HList.objects.values_list('List_ID')
>>> Dlists.union(HLists)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "F:\Program Files\Python310\lib\site-packages\django\db\models\query.py", line 370, in __repr__
data = list(self[: REPR_OUTPUT_SIZE + 1])
File "F:\Program Files\Python310\lib\site-packages\django\db\models\query.py", line 376, in __len__
self._fetch_all()
File "F:\Program Files\Python310\lib\site-packages\django\db\models\query.py", line 1866, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "F:\Program Files\Python310\lib\site-packages\django\db\models\query.py", line 242, in __iter__
return compiler.results_iter(
File "F:\Program Files\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1346, in results_iter
results = self.execute_sql(
File "F:\Program Files\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1382, in execute_sql
sql, params = self.as_sql()
File "F:\Program Files\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 605, in as_sql
result, params = self.get_combinator_sql(
File "F:\Program Files\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 520, in get_combinator_sql
raise DatabaseError(
django.db.utils.DatabaseError: ORDER BY not allowed in subqueries of compound statements.