I'm using SQLAlchemy with SQLite and attempting to filter by Dates, to see if a similar obj is already in a session, or not.
from sqlalchemy import func
from sqlalchemy.types import DATE, Date, DATETIME, DateTime, TIME, Time, TIMESTAMP
date_types = (DATE, Date, DATETIME, DateTime, TIME, Time, TIMESTAMP)
def asdict(table):
keys = table.__table__.columns.keys()
values = [getattr(table, key) for key in keys]
return dict(zip(keys, values))
filter_kwargs = {k: v for k, v in asdict(table).items() if v is not None}
for c in table.__table__.columns:
if isinstance(c.type, date_types):
filter_kwargs[c.name] = func.DATE(c)
exists = session.query(table.__class__).filter_by(**filter_kwargs).first()
Using func.Date() is the solution given in these two discussions:
https://gist.github.com/danielthiel/8374607
Comparing Dates with SQLAlchemy, SQLite
I must be using it wrong somehow though... because exists is always None. Any help is appreciated. Thank you.