Skip to content

feat: add back as_query method to AwaitableQuery#2194

Open
seladb wants to merge 2 commits into
tortoise:developfrom
seladb:add-back-as-query
Open

feat: add back as_query method to AwaitableQuery#2194
seladb wants to merge 2 commits into
tortoise:developfrom
seladb:add-back-as-query

Conversation

@seladb
Copy link
Copy Markdown
Contributor

@seladb seladb commented May 15, 2026

Description

Addresses #2056: bring back AwaitableQuery.as_query().

Motivation and Context

As described in #2056 there are cases where users have complex queries and need access to the internal pypika layer. The as_query() method used to exist but was removed in #1777. I don't see a reason why not to add it back.

How Has This Been Tested?

Yes, tests added int this PR.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 15, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing seladb:add-back-as-query (cb0d601) with develop (c0de49f)

Open in CodSpeed

@seladb seladb changed the title Add back as_query method to AwaitableQuery feat: add back as_query method to AwaitableQuery May 15, 2026
@seladb
Copy link
Copy Markdown
Contributor Author

seladb commented May 27, 2026

@abondar @waketzheng can you please review this PR?

@waketzheng
Copy link
Copy Markdown
Contributor

@seladb Thanks for your work.

The general direction makes sense: #2056 describes a legitimate escape-hatch use case where users need to reuse the SQL/PyPika query already built by Tortoise and then compose something that the ORM API does not currently support well.

One thing worth considering is whether as_query() should be added back on AwaitableQuery itself.

AwaitableQuery is a very broad base class, and not every subclass naturally maps to a single reusable QueryBuilder. For example, bulk operations may produce multiple statements, and raw SQL does not really have an internal PyPika query to expose in the same way. Adding the method at the base class level makes it look like this is a stable capability for every awaitable query type, which may create a wider public API contract than intended.

A slightly safer approach might be:

  • Add as_query() only to query classes that can reliably return a single PyPika QueryBuilder (QuerySet, ValuesQuery, ValuesListQuery, CountQuery, ExistsQuery, and possibly normal UpdateQuery / DeleteQuery).
  • Document it as an advanced escape hatch rather than a primary ORM API.
  • Encourage use with get_parameterized_sql() / execute_pypika() where possible, rather than encouraging users to manually execute get_sql().
  • Add a test that covers the actual use case from The as_query method in the queryset.py file has been removed #2056, e.g. composing a query with qs.as_query().join(other_qs.as_query()).on(...).
  • Add tests or explicit behavior for unsupported query types, so users do not assume every awaitable query can be converted into a single PyPika query.

Overall this seems like a useful capability to restore, but it may be worth keeping the public API surface narrower than adding it directly to AwaitableQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants