SQLAlchemy 自定义布尔值
我有一个以这种方式实现布尔逻辑的 Oracle 数据库:
MYCOL NUMBER(1,0)
0 = False
-1 or 1 = True
NULL = None
使用默认的 SQLAlchemy 布尔列,-1 表值与 False
或 True
不匹配。 是否可以创建一个 SQLAlchemy 自定义类型,将此类列映射到 bool python 属性?
困难在于有多个与 true
关联的值。 例如:
session.query(User).filter_by(active=True)
应该生成以下查询之一:
select * from user where active != 0;
select * from user where active in (-1, 1);
select * from user where 1 = (CASE WHEN active != 0 THEN 1 ELSE active end);
I have an Oracle database with boolean logic implemented this way:
MYCOL NUMBER(1,0)
0 = False
-1 or 1 = True
NULL = None
With a default SQLAlchemy boolean column, -1 table value doesn't match False
or True
.
Is it possible to create an SQLAlchemy custom type that maps such a column to a bool python attribute?
The difficulty relies in having multiple values associated with true
.
Ex:
session.query(User).filter_by(active=True)
Should generate one of these queries:
select * from user where active != 0;
select * from user where active in (-1, 1);
select * from user where 1 = (CASE WHEN active != 0 THEN 1 ELSE active end);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此实现似乎适用于基本查询。
Case when
表达式似乎是最简单的解决方案,因为运算符逻辑没有改变。This implementation seems to work for basic queries.
Case when
expression seems to be the easiest solution because operator logic isn't altered.