Since my initial problem with the .values
inside the components of UNION I tried removing that to see what would happen. I got an error which currently I cannot reproduce in the dummy repository which I setup for sharing (if I got more time I will try to adjust the setup to see how to reproduce it).
These are the latest frames of the stacktrace:
...
if len(data) > 1:
secondary_qs = secondary_qs.order_by('-timestamp')
secondary_qs = secondary_qs.values('main')[:1]
with suppress(MainModel.DoesNotExist):
return MainModel.objects.filter(customer=customer).get(pk=secondary_qs)
../.venv/lib/python3.12/site-packages/django/db/models/query.py:645: in get
num = len(clone)
args = ()
clone = <[FieldError("Cannot resolve keyword '' into field. Choices are: main, main_id, id, timestamp, type, value") raised in repr()] QuerySet object at 0x797a86982360>
kwargs = <[FieldError("Cannot resolve keyword '' into field. Choices are: main, main_id, id, timestamp, type, value") raised in repr()] dict object at 0x797a86aed540>
limit = 21
self = <QuerySet [<MainModel: 9a99e1c4-adea-46a0-ad0c-67f93340a3a3>, <MainModel: bd64a09d-0e2c-4f71-841b-819434fdcb58>, <MainModel: e9afd7ac-589b-4510-8d52-e4989ba0c56e>, <MainModel: 753d4f5b-f406-400d-88f0-18f9708ebf66>, <MainModel: 753d4f5b-f406-401d-88f0-18f9708ebf66>]>
../.venv/lib/python3.12/site-packages/django/db/models/query.py:382: in __len__
self._fetch_all()
self = <[FieldError("Cannot resolve keyword '' into field. Choices are: main, main_id, id, timestamp, type, value") raised in repr()] QuerySet object at 0x797a86982360>
../.venv/lib/python3.12/site-packages/django/db/models/query.py:1928: in _fetch_all
self._result_cache = list(self._iterable_class(self))
self = <[FieldError("Cannot resolve keyword '' into field. Choices are: main, main_id, id, timestamp, type, value") raised in repr()] QuerySet object at 0x797a86982360>
../.venv/lib/python3.12/site-packages/django/db/models/query.py:91: in __iter__
results = compiler.execute_sql(
compiler = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
db = 'default'
queryset = <[FieldError("Cannot resolve keyword '' into field. Choices are: main, main_id, id, timestamp, type, value") raised in repr()] QuerySet object at 0x797a86982360>
self = <django.db.models.query.ModelIterable object at 0x797a86980890>
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:1561: in execute_sql
sql, params = self.as_sql()
chunk_size = 100
chunked_fetch = False
result_type = 'multi'
self = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:775: in as_sql
self.compile(self.where) if self.where is not None else ("", [])
combinator = None
distinct_fields = []
distinct_params = []
extra_select = []
f_params = []
features = <django.db.backends.postgresql.features.DatabaseFeatures object at 0x797a8b20dee0>
for_update_part = None
from_ = ['"test_union_main"']
group_by = []
order_by = []
refcounts_before = {'test_union_main': 2, 'portal_customer': 0}
self = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
with_col_aliases = False
with_limit_offset = True
with_limits = True
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:557: in compile
sql, params = node.as_sql(self, self.connection)
node = <WhereNode: (AND: RelatedExact(Col(test_union_main, test_union.MainModel.customer), 50001), Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>))>
self = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
vendor_impl = None
../.venv/lib/python3.12/site-packages/django/db/models/sql/where.py:151: in as_sql
sql, params = compiler.compile(child)
child = Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>)
compiler = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
connection = <DatabaseWrapper vendor='postgresql' alias='default'>
empty_needed = 1
full_needed = 2
params = [50001]
result = ['"test_union_main"."customer_id" = %s']
result_params = [50001]
self = <WhereNode: (AND: RelatedExact(Col(test_union_main, test_union.MainModel.customer), 50001), Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>))>
sql = '"test_union_main"."customer_id" = %s'
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:557: in compile
sql, params = node.as_sql(self, self.connection)
node = Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>)
self = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
vendor_impl = None
../.venv/lib/python3.12/site-packages/django/db/models/lookups.py:385: in as_sql
return super().as_sql(compiler, connection)
__class__ = <class 'django.db.models.lookups.Exact'>
compiler = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
connection = <DatabaseWrapper vendor='postgresql' alias='default'>
self = Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>)
../.venv/lib/python3.12/site-packages/django/db/models/lookups.py:246: in as_sql
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
compiler = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
connection = <DatabaseWrapper vendor='postgresql' alias='default'>
lhs_sql = '"test_union_main"."id"'
params = []
self = Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>)
../.venv/lib/python3.12/site-packages/django/db/models/lookups.py:121: in process_rhs
sql, params = compiler.compile(value)
compiler = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
connection = <DatabaseWrapper vendor='postgresql' alias='default'>
self = Exact(Col(test_union_main, test_union.MainModel.id), <django.db.models.sql.query.Query object at 0x797a86982090>)
value = <django.db.models.sql.query.Query object at 0x797a86982090>
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:557: in compile
sql, params = node.as_sql(self, self.connection)
node = <django.db.models.sql.query.Query object at 0x797a86982090>
self = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
vendor_impl = None
../.venv/lib/python3.12/site-packages/django/db/models/sql/query.py:1263: in as_sql
sql, params = self.get_compiler(connection=connection).as_sql()
compiler = <SQLCompiler model=MainModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using='default'>
connection = <DatabaseWrapper vendor='postgresql' alias='default'>
self = <django.db.models.sql.query.Query object at 0x797a86982090>
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:762: in as_sql
result, params = self.get_combinator_sql(
combinator = 'union'
extra_select = []
features = <django.db.backends.postgresql.features.DatabaseFeatures object at 0x797a8b20dee0>
for_update_part = None
group_by = []
order_by = [(OrderBy(Ref(__orderbycol2, Col(U0, test_union.SecondaryModel.timestamp)), descending=True),
('"__orderbycol2" DESC', [], False))]
refcounts_before = {'U0': 4, 'U1': 1, 'U2': 0}
self = <SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>
with_col_aliases = False
with_limit_offset = True
with_limits = True
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:598: in get_combinator_sql
part_sql, part_args = compiler.as_sql(with_col_aliases=True)
all = False
combinator = 'union'
compiler = <SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>
compilers = [<SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>,
<SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>]
features = <django.db.backends.postgresql.features.DatabaseFeatures object at 0x797a8b20dee0>
parts = ()
self = <SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:762: in as_sql
result, params = self.get_combinator_sql(
combinator = 'union'
extra_select = []
features = <django.db.backends.postgresql.features.DatabaseFeatures object at 0x797a8b20dee0>
for_update_part = None
group_by = []
order_by = []
refcounts_before = {'U0': 5, 'U1': 1, 'U2': 0}
self = <SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>
with_col_aliases = True
with_limit_offset = False
with_limits = True
../.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py:591: in get_combinator_sql
compiler.query.set_values(
all = False
combinator = 'union'
compiler = <SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>
compilers = [<SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>,
<SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>]
features = <django.db.backends.postgresql.features.DatabaseFeatures object at 0x797a8b20dee0>
parts = ()
self = <SQLCompiler model=SecondaryModel connection=<DatabaseWrapper vendor='postgresql' alias='default'> using=None>
../.venv/lib/python3.12/site-packages/django/db/models/sql/query.py:2500: in set_values
self.add_fields(field_names, True)
annotation_names = []
extra_names = []
field = '__orderbycol2'
field_names = ['main', '__orderbycol2']
fields = ('main', '__orderbycol2')
selected = frozenset({'main', '__orderbycol2'})
self = <django.db.models.sql.query.Query object at 0x797a86983170>
../.venv/lib/python3.12/site-packages/django/db/models/sql/query.py:2195: in add_fields
join_info = self.setup_joins(
alias = 'U0'
allow_m2m = True
cols = [Col(U0, test_union.SecondaryModel.main)]
field_names = ['main', '__orderbycol2']
final_alias = 'U0'
join_info = JoinInfo(final_field=<django.db.models.fields.related.ForeignKey: main>, targets=(<django.db.models.fields.UUIDField: id>,), opts=<Options for MainModel>, joins=['U0', 'U1'], path=[PathInfo(from_opts=<Options for SecondaryModel>, to_opts=<Options for MainModel>, target_fields=(<django.db.models.fields.UUIDField: id>,), join_field=<django.db.models.fields.related.ForeignKey: main>, m2m=False, direct=True, filtered_relation=None)], transform_function=<function Query.setup_joins.<locals>.final_transformer at 0x797a86955d00>)
joins = ['U0']
name = '__orderbycol2'
opts = <Options for SecondaryModel>
self = <django.db.models.sql.query.Query object at 0x797a86983170>
target = <django.db.models.fields.related.ForeignKey: main>
targets = (<django.db.models.fields.related.ForeignKey: main>,)
../.venv/lib/python3.12/site-packages/django/db/models/sql/query.py:1863: in setup_joins
path, final_field, targets, rest = self.names_to_path(
alias = 'U0'
allow_many = True
can_reuse = None
final_transformer = <function Query.setup_joins.<locals>.final_transformer at 0x797a86955c60>
joins = ['U0']
last_field_exception = FieldError("Cannot resolve keyword '' into field. Choices are: main, main_id, id, timestamp, type, value")
names = ['', 'orderbycol2']
opts = <Options for SecondaryModel>
pivot = 1
self = <django.db.models.sql.query.Query object at 0x797a86983170>
The colalias
calculated here is getting considered as a lookup for a field since it contains a double underscore.