I got a weird problem when I was doing the time range filtering in sqlalchemy. There is a datetime column in Event table called startDate. I was writing the query to return all the events that have the starting time between 6pm and 11:59pm (doesn't matter which date).
I have the following code,
from sqlalchemy import and_, Date, Time, cast
import datetime
startingTime = datetime.time(18, 0, 0)
endingTime = datetime.time(23, 59, 59)
If I run,
results = session.query(Event).filter(and_(cast(Event.startDate, Time)>=startingTime, cast(Event.startDate, Time)<=endingTime).all()
I got no result (for sure there are events in this range).
But if I run,
results = session.query(Event).filter(cast(Event.startDate, Time)>=endingTime).all()
It returns all rows in the table.
If I filter by dates,
results = session.query(Event).filter(and_(cast(Event.startDate, Date)>=one_date, cast(Event.startDate, Date)<=another_date).all()
results are correct!
Let's say the startDate of Event_A is 2013-11-27T19:30:00, and
result = session.query(cast(Event.startDate, Time)).filter(Event.eventName=='Event_A').one()[0]
result >= startingTime ----> returns True
result <= endingTime ----> returns True
Any help would be appreciated.
{}button in the text editor toolbar to do this.SA querythat you showed you would also add the resultingSQLstatement. You can see this by enabling logging via passing settingengine.echo=True.