为什么SQLAlchemy中不可能将布尔类型列用作ClauseElement本身?
session.query(Table).filter(Table.name == 'huszar', Table.valid)
当然Table.valid == True
会起作用,但对我来说看起来有点难看......
答案 0 :(得分:2)
我想也许你在0.7上并且ORM在单个filter()调用中还没有支持多个标准,这是在0.8中添加的,如果是独立的话,0.7似乎也需要表绑定列。一切都在0.8:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
data = Column(String(20))
boolean = Column(Boolean)
# works
e = create_engine("sqlite://", echo=True)
# works
#e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
# works
#e = create_engine("mysql://scott:tiger@localhost/test", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add_all([
A(data='a1', boolean=True),
A(data='a2', boolean=False),
A(data='a3', boolean=True),
])
# works
print s.query(A).filter(A.data > 'a1', A.boolean).all()
# works
print s.query(A).filter(A.boolean).all()
# if before 0.8, need to use and_() or table-bound column
# print s.query(A).filter(and_(A.data > 'a1', A.boolean)).all()
# print s.query(A).filter(A.__table__.c.boolean).all()