简体   繁体   English

SQLAlchemy 过滤器将 datetime.time 转换为 datetime.datetime

[英]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.

相关问题 在 datetime.datetime 中转换 datetime.time - Convert datetime.time in datetime.datetime 类型错误:无法将 datetime.datetime 与 datetime.time 进行比较 - TypeError: can't compare datetime.datetime to datetime.time 使减法成为可能:“ datetime.time”和“ datetime.datetime” - Making the subtraction posssible: 'datetime.time' and 'datetime.datetime' 将datetime.datetime与datetime.time相结合 - TypeError:需要一个整数 - combining datetime.datetime with datetime.time - TypeError: an integer is required 元组中的 datetime.time 显示 datetime.datetime 而不是值 - datetime.time in a tuple shows datetime.datetime instead value Pandas - 将 int 转换为 datetime.time 和 datetime.datetime - Pandas - convert int to datetime.time and datetime.datetime 熊猫:修复datetime.time和datetime.datetime混合 - Pandas: Fixing datetime.time and datetime.datetime mix 在“ datetime.time”和“ datetime.datetime”的实例之间不支持“>” - '>' not supported between instances of 'datetime.time' and 'datetime.datetime' 在“ datetime.datetime”和“ datetime.time”的实例之间不支持“> =” - '>=' not supported between instances of 'datetime.datetime' and 'datetime.time' 如何将Excel列读取为datetime.datetime而不是自动datetime.time - How do I read an Excel column as datetime.datetime instead of the auto datetime.time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM