Conversation
# Conflicts: # tortoise/queryset.py
| assert result == expected | ||
|
|
||
|
|
||
| @requireCapability(dialect=NotEQ("mssql")) |
There was a problem hiding this comment.
MSSql limit syntax is different but it's not currently supported in _SetOperation which has its own implementation of _limit_sql() instead of using the dialect SQL. I guess we can leave it as a TODO for later
themavik
left a comment
There was a problem hiding this comment.
Reviewed the changes — the implementation is clean and follows the existing patterns.
| @classmethod | ||
| def _get_selects(cls, qs: QuerySet[Model] | UnionQuery[Model]) -> list[str]: | ||
| return [ | ||
| select.name |
There was a problem hiding this comment.
That will probably fail on query with .annotate(..) - if we don't support them we would need some clean error about that, not attribute error
|
|
||
| self._union_query = self._union_query.orderby(field_name, order=order) | ||
|
|
||
| if self._limit is not None: |
There was a problem hiding this comment.
shouldn't we support offset too in such case? or there are some limitations?
| union = self._clone() | ||
| union._models = {*union._models, *(qs.model for qs in other_qs)} | ||
| union._qs = union._qs + other_qs | ||
| union._all = all |
There was a problem hiding this comment.
that would overwrite existing _all from previous union call - is it intended behavior?
|
|
||
| .order_by('name', '-id') | ||
|
|
||
| Supports ordering by related models too. |
There was a problem hiding this comment.
does it really support ordering by related models?
| self._selects = self._get_selects(qs) | ||
| else: | ||
| if self._get_selects(qs) != self._selects: | ||
| raise ValueError("Union queries must have the same select fields") |
| Return count of objects in union query. | ||
| """ | ||
| self._choose_db_if_not_chosen() | ||
| self._make_query() |
There was a problem hiding this comment.
There is issue here, that count mutates query, and if you will have code like
asyncio.gather(union_query, union_query.count()) - it will apply _make_query two times on same, which could fail
Add union query support in an API similar to Django's API
Addresses these issues:
#1507
#2056 (partially)
Description
Add union query support:
As opposed to Django, this API supports result of multiple models (in Django all results will be instances of the first model).
However, similar to Django, the requested field names and types should be the same across all queried models. This is because of how DBs support union queries.
Motivation and Context
It addresses the following issues:
#1507
#2056 (partially)
Also - it brings more feature parity with Django.
How Has This Been Tested?
Added test coverage for all written code. It was tested with MySQL, Postgres and SQLite.
Checklist: