I am using SQLAlchemy and cannot for the life of me get it to filter a TIME column without treating it as a DATETIME.
I have the class defined as
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
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
short term solution is to use cast:
from sqlalchemy import cast
query.filter(my_column >= cast(datetime.time(5, 0), TIME))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.