python - Sqlalchemy: joinedload + limit

标签 python sql sqlalchemy subquery limit

给出以下语句:

p = db.query(Profile).options(joinedload('*')).filter_by(id=p.id).limit(1).one()

我将得到一个子查询 + 一个连接,而不是一个“纯”连接:

SELECT [...] 
FROM (SELECT profile.id AS profile_id, ...
FROM profile 
WHERE profile.id = %(id_1)s 
LIMIT %(param_1)s) AS anon_1 LEFT OUTER JOIN city AS city_1 ON city_1.id = anon_1.profile_city LEFT OUTER JOIN country AS country_1 ON country_1.id = city_1.country LEFT OUTER JOIN state AS state_1 ON country_1.id = state_1.country LEFT OUTER JOIN state AS state_2 ON state_2.id = city_1.state LEFT OUTER JOIN country AS country_2 ON country_2.id = state_2.country LEFT OUTER JOIN state AS state_3 ON state_3.id = city_1.state LEFT OUTER JOIN country AS country_3 ON country_3.id = state_3.country LEFT OUTER JOIN starred AS starred_1 ON anon_1.profile_id = starred_1.star LEFT OUTER JOIN profiletext AS profiletext_1 ON anon_1.profile_id = profiletext_1.profile LEFT OUTER JOIN starred AS starred_2 ON anon_1.profile_id = starred_2.idprofile LEFT OUTER JOIN photo AS photo_1 ON anon_1.profile_id = photo_1.profile LEFT OUTER JOIN gps AS gps_1 ON anon_1.profile_id = gps_1.idprofile

但我真正需要的是:

SELECT ...
FROM profile LEFT OUTER JOIN city AS city_1 ON city_1.id = profile.city LEFT OUTER JOIN country AS country_1 ON country_1.id = city_1.country LEFT OUTER JOIN state AS state_1 ON country_1.id = state_1.country LEFT OUTER JOIN state AS state_2 ON state_2.id = city_1.state     
LEFT OUTER JOIN country AS country_2 ON country_2.id = state_2.country LEFT OUTER JOIN state AS state_3 ON state_3.id = city_1.state LEFT OUTER JOIN country AS country_3 ON country_3.id = state_3.country LEFT OUTER JOIN starred AS starred_1 ON profile.id = starred_1.star LEFT OUTER JOIN profiletext AS profiletext_1 ON profile.id = profiletext_1.profile LEFT OUTER JOIN starred AS starred_2 ON profile.id = starred_2.idprofile LEFT OUTER JOIN photo AS photo_1 ON profile.id = photo_1.profile LEFT OUTER JOIN gps AS gps_1 
ON profile.id = gps_1.idprofile                                                                                                                                                                                                    
WHERE profile.id = 4 
limit 1;

即没有子查询。

数据库:postgresql 9.2

最佳答案

根据 The Zen of Eager Loading 这似乎是预期的行为

When using joined eager loading, if the query contains a modifier that impacts the rows returned externally to the joins, such as when using DISTINCT, LIMIT, OFFSET or equivalent, the completed statement is first wrapped inside a subquery, and the joins used specifically for joined eager loading are applied to the subquery. SQLAlchemy’s joined eager loading goes the extra mile, and then ten miles further, to absolutely ensure that it does not affect the end result of the query, only the way collections and related objects are loaded, no matter what the format of the query is.

我意识到这是一个老问题,但是生成的 SQL 不起作用是否有原因?

关于python - Sqlalchemy: joinedload + limit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553220/

相关文章:

python - SQLAlchemy,Python 我想按从一天开始到一天结束的创建日期来过滤我的模型

单元测试中的 SQLAlchemy session 投票

python - 如何指定一系列 unicode 字符

sql - sql tsql 中的毫秒

Python/PyCharm 标记 "unused"import as used

mysql - 如何在 SQL 上正确计算帐户余额

mysql 查询 3 个表,join 或 union 哪个最适合计算余额?

python - 如何使用 SQLAlchemy 的 'execute' 插入 JSONB?

python - 创建列表列表。修改列表中的元素

python - py2app 在 Anaconda Python 3 上失败