[英]SQLAlchemy filter converting datetime.time to datetime.datetime
I am using SQLAlchemy and cannot for the life of me get it to filter a TIME column without treating it as a DATETIME.我正在使用 SQLAlchemy 并且不能在我的一生中让它过滤 TIME 列而不将其视为 DATETIME。
I have the class defined as我将 class 定义为
class v_MyView(Base):
__table__ = Table('MyView', Base.metadata,
Column('id', Integer, primary_key=True),
Column('EntryDateTime', DateTime),
Column('EntryDate', Date),
Column('EntryTime', Time),
...
If I do this如果我这样做
results = db.query(v_MyView).first()
print(results.EntryTime)
I get datetime.time(15, 30, 22, 560000)
as expected我按预期得到datetime.time(15, 30, 22, 560000)
However if I do this但是,如果我这样做
results = db.query(v_MyView).filter(v_MyView.EntryTime >= datetime.time(5,0)).first()
print(results.EntryTime)
I get an error我收到一个错误
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The data types time and datetime2
are incompatible in the greater than or equal to operator. (402) (SQLExecDirectW)
And looking at the generated query, I see并查看生成的查询,我看到
WHERE [v_MyView].[EntryTime] >= ?]
[parameters: (datetime.datetime(1900, 1, 1, 5, 0),)]
Examining the filter it appears to be generated correctly检查过滤器,它似乎是正确生成的
right: BindParameter('%(140718484304128 EntryTime)s', datetime.time(5, 0), type_=Time())
Am I missing something or is this a bug?我错过了什么还是这是一个错误?
This has been accepted as a bug in the SQLAlchemy repo这已被接受为SQLAlchemy 存储库中的错误
short term solution is to use cast:短期解决方案是使用演员表:
from sqlalchemy import cast
query.filter(my_column >= cast(datetime.time(5, 0), TIME))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.