Add exists filter syntax
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
调研方向
从相关 issue #385 开始,将提议的过滤语法与此 issue 中的 EXISTS 和 JOIN SQL 示例进行比较。明确 __exists 应该放在哪里,以及它的语义应该是什么;完成的标准是达成一种共识语法,能够生成基于存在性的过滤,而无需使用 distinct()。
由索引模型根据 Issue 内容生成。
描述
Is your feature request related to a problem? Please describe.
When querying for the existence of a one-to-many related model with certain values it one of its fields, the .distinct() method is required to prevent duplicates. This is not the most efficient query.
Contrived example: I have to do this to find all phone numbers of users that work at the north pole without duplicates:
PhoneNumber.filter(user__company__location='North Pole').distinct()
According to ChatGPT,
SELECT m.*
FROM model m
WHERE EXISTS (
SELECT 1
FROM user u
JOIN company c ON u.company_id = c.id
WHERE u.id = m.user_id
AND c.location = 'North Pole'
);
is preferable over
SELECT DISTINCT m.*
FROM model m
LEFT OUTER JOIN user u ON m.user_id = u.id
LEFT OUTER JOIN company c ON u.company_id = c.id
WHERE c.location = 'North Pole';
Why this is better (copied from ChatGPT):
EXISTSchecks for existence rather than joining, so no duplicates.- Often more efficient, since the DB can stop searching after the first match.
- Cleaner semantics: “Does this related row exist?” rather than “Give me all the related rows, then deduplicate.”
Describe the solution you'd like
PhoneNumber.filter(user__company__exists__location='North Pole')
Describe alternatives you've considered
Could be variations on the placement of __exists in the query. I realize the exact semantics could be a bit unclear.
Additional context
Related #385
- 主要语言
- Python
- 星标
- 5.6k
- 派生
- 523
- 平均合并
- 2 天 18 小时
- 30 天内合并 PR
- 12
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tortoise/tortoise-orm 的其他 Issue
-
pydantic_model_creator returns a cached class that ignores model_config, validators and module可能已有人在做 @moneebullah25 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
tortoise/tortoise-orm#2310 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
tortoise/tortoise-orm#668 ·
维护者通常 2 天内回复
-
refresh_from_db silently ignores fields supplied as an iterator可能已有人在做 @IlyaKhalafi 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
tortoise/tortoise-orm#2341 ·
维护者通常 2 天内回复
-
Renamed fields break dict(model) and default values_list可能已有人在做 @IlyaKhalafi 于 1 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 25/100
tortoise/tortoise-orm#2337 ·
维护者通常 2 天内回复
-
Reverse relation filters use attribute names for SQL columns可能已有人在做 @IlyaKhalafi 于 1 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 25/100
tortoise/tortoise-orm#2336 ·
维护者通常 2 天内回复
查看 tortoise/tortoise-orm 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 72/100
aws-samples/sample-per-model-bedrock#261 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
pwncollege/dojo#1188 ·
维护者通常 1 天内回复
-
bug launcher packaging
难度 1/5 1 小时以内 新手友好度 88/100
tenstorrent/tt-studio#1426 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
python-attrs/cattrs#796 ·
维护者通常 1 天内回复