Skip to content

Commit 9b9ddcf

Browse files
committed
Fix collection in unions when a table alias exists.
1 parent ba4c521 commit 9b9ddcf

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

django_mongodb/compiler.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,16 @@ def project_field(column):
434434
)
435435

436436
@cached_property
437-
def collection_name(self):
438-
base_table = next(
437+
def base_table(self):
438+
return next(
439439
v
440440
for k, v in self.query.alias_map.items()
441441
if isinstance(v, BaseTable) and self.query.alias_refcount[k]
442442
)
443-
return base_table.table_alias or base_table.table_name
443+
444+
@cached_property
445+
def collection_name(self):
446+
return self.base_table.table_alias or self.base_table.table_name
444447

445448
@cached_property
446449
def collection(self):
@@ -469,6 +472,7 @@ def get_combinator_queries(self):
469472
)
470473
)
471474
compiler_.pre_sql_setup()
475+
compiler_.column_indices = self.column_indices
472476
columns = compiler_.get_columns()
473477
parts.append((compiler_.build_query(columns), compiler_, columns))
474478
except EmptyResultSet:
@@ -496,7 +500,12 @@ def get_combinator_queries(self):
496500
# Combine query with the current combinator pipeline.
497501
if combinator_pipeline:
498502
combinator_pipeline.append(
499-
{"$unionWith": {"coll": compiler_.collection_name, "pipeline": inner_pipeline}}
503+
{
504+
"$unionWith": {
505+
"coll": compiler_.base_table.table_name,
506+
"pipeline": inner_pipeline,
507+
}
508+
}
500509
)
501510
else:
502511
combinator_pipeline = inner_pipeline

0 commit comments

Comments
 (0)