SQLMODEL:NULL过滤器

发布于 2025-02-11 00:43:28 字数 657 浏览 6 评论 0 原文

在此处参考过滤示例:,我如何获取所有年龄段的英雄。

我需要以下等效的:

select * from hero where age is null

这是:

select(Hero).where(Hero.age != None)

但是,IDE抱怨 PEP 8:e711与None的比较应该是'如果cond不是一个:'

为:

select(Hero).where(Hero.age is None)

,我将其更改 预期导致不正确的SQL生成:

SELECT * FROM hero WHERE 0 = 1

正确的方法是什么?

Referring to the filtering examples here: https://sqlmodel.tiangolo.com/tutorial/where/#filter-rows-using-where-with-sqlmodel, how do I fetch all heroes whose age is null.

I need the equivalent of:

select * from hero where age is null

This works:

select(Hero).where(Hero.age != None)

but, IDE complains PEP 8: E711 comparison to None should be 'if cond is not None:'

So I changed it to:

select(Hero).where(Hero.age is None)

but, it does not work as intended causing an incorrect SQL to generate:

SELECT * FROM hero WHERE 0 = 1

What is the right approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2025-02-18 00:43:29

此SQLModel问题仍然是 open 。它尚未合并到代码库。

我建议以下代码:

select(Team).where(
            Team.heros == None,  # noqa E711 Comparison to `None` should be `cond is None`.
)
select(Team).where(
            Team.heros is None,  # This does not work as intended.
)

This SQLModel issue is still open. It hasn't be merged to the code base yet.

I recommend the following code:

select(Team).where(
            Team.heros == None,  # noqa E711 Comparison to `None` should be `cond is None`.
)
select(Team).where(
            Team.heros is None,  # This does not work as intended.
)
风筝有风,海豚有海 2025-02-18 00:43:28
from sqlalchemy.sql.operators import is_


stmt = select(Hero).where(is_(Hero.age, None))
result = session.exec(stmt)

from sqlalchemy.sql.operators import is_


stmt = select(Hero).where(is_(Hero.age, None))
result = session.exec(stmt)

娇妻 2025-02-18 00:43:28

该解决方案不需要额外的导入,因为是_ 方法是一个属性:(

stmt = select(Hero).where(Hero.age.is_(None))
result = session.exec(stmt)

来源

This solution does not require an additional import, as the is_ method is an attribute:

stmt = select(Hero).where(Hero.age.is_(None))
result = session.exec(stmt)

(Source)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文