加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – sqlalchemy:null比较交换性

发布时间:2020-12-20 13:52:38 所属栏目:Python 来源:网络整理
导读:我只是偶然发现了sqlalchemy的这种行为: from sqlalchemy import Column,null print(Column("x") == null())x IS NULL print(Column("x") == None)x IS NULL print(null() == Column("x"))NULL = x print(None == Column("x"))x IS NULL 有人可以解释为什么
我只是偶然发现了sqlalchemy的这种行为:

>>> from sqlalchemy import Column,null

>>> print(Column("x") == null())
x IS NULL
>>> print(Column("x") == None)
x IS NULL

>>> print(null() == Column("x"))
NULL = x
>>> print(None == Column("x"))
x IS NULL

有人可以解释为什么在使用None时,==对于null()是不可交换的?目的是什么?

解决方法

首先,请查看 Identity vs. Equailty,了解为什么None条件都返回x IS NULL.

null()子句返回不同结果的原因是运算符的工作方式.

根据文档,运算符的工作方式如下:

In a column context,produces the clause a = b. If the target is None,produces a IS NULL.

说明:

Column("x") == null() # a = b -> b is NullType; returning IS NULL

null() == Column('x') # a = b -> b is ColumnType; not returning IS NULL

有关更多信息,请查看Sqlalchemy Docs

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读