python - Using a SQLAlchemy Integer field to create a timedelta object for comparison -
i have object called item. has 2 fields: datetime row called "created_on," , integer row called "days."
i want query objects created "days" many days ago.
here's how thought should done:
now = utcnow() session.query(item).filter(item.created_on + interval(timedelta(days=item.days)) <= now)
but i'm unable create timedelta this. i'm getting error:
typeerror: unsupported type timedelta minutes component: instrumentedattribute
update:
thanks van, should using built-in function. i'm using mysql 5.1, timestampadd
. new query this:
now = utcnow() session.query(item).filter(func.timestampadd('day', item.days, item.created_on) <= now)
however, new error i'm getting is:
"you have error in sql syntax; check manual corresponds mysql server version right syntax use near ''day', items.days, items.created_on) <= '2015-07-12 ' @ line 3"
i not think interval
there, not able coerce db-specific date[time] functions. should use platform specific date[time] functions solve this.
if using postgresql, code below should work (assuming items.days
integer):
q = (session.query(item) .filter(func.age(now, item.created_on) <= func.make_interval(0, 0, 0, item.days) ) )
see postgres'
date/time functions , operators more information.
Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
ReplyDeletesql server dba online training
sql database administrator training