[英]SQLAlchemy Nested CTE Query
The sqlalchemy core query builder appears to unnest and relocate CTE queries to the "top" of the compiled sql. sqlalchemy 核心查询生成器似乎取消嵌套 CTE 查询并将其重新定位到已编译 sql 的“顶部”。
I'm converting an existing Postgres query that selects deeply joined data as a single JSON object.我正在转换一个现有的 Postgres 查询,该查询选择深度连接的数据作为单个 JSON object。 The syntax is pretty contrived but it significantly reduces network overhead for large queries.
语法相当做作,但它显着减少了大型查询的网络开销。 The goal is to build the query dynamically using the sqlalchemy core query builder.
目标是使用 sqlalchemy 核心查询构建器动态构建查询。
Here's a minimal working example of a nested CTE这是嵌套 CTE 的最小工作示例
with res_cte as (
select
account_0.name acct_name,
(
with offer_cte as (
select
offer_0.id
from
offer offer_0
where
offer_0.account_id = account_0.id
)
select
array_agg(offer_cte.id)
from
offer_cte
) as offer_arr
from
account account_0
)
select
acct_name::text, offer_arr::text
from res_cte
Result结果
acct_name, offer_arr
---------------------
oliver, null
rachel, {3}
buddy, {4,5}
(my incorrect use of) the core query builder attempts to unnest offer_cte
and results in every offer.id
being associated with every account_name
in the result. (我的错误使用)核心查询构建器尝试
offer_cte
并导致每个offer.id
与结果中的每个account_name
相关联。
There's no need to re-implement this exact query in an answer, any example that results in a similarly nested CTE would be perfect.无需在答案中重新实现这个确切的查询,任何导致类似嵌套 CTE 的示例都是完美的。
Received confirmation from the sqla team that it is not currently possible or on any roadmap https://github.com/sqlalchemy/sqlalchemy/issues/5335#issuecomment-630838687收到 sqla 团队的确认,即目前不可能或在任何路线图https://github.com/sqlalchemy/sqlalchemy/issues/5335#issuecomment-630838687
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.