A
A
Avrong2016-01-10 10:48:36
SQLite
Avrong, 2016-01-10 10:48:36

Why is the filter for select query not working correctly in SQLAlchemy (Flask)?

There is a request...

User.query.filter((datetime.datetime.now()-User.latest_active)<datetime.timedelta(minutes=1)).count()

...which should select from the database the number of users active on the site less than 1 minute ago. Works without syntax errors. However, it selects all (2) users even though one of them was last online 2 weeks ago.

If you check the second (2 weeks ago which) user manually:
user = User.query.filter_by(id=2).first()
print((datetime.datetime.now()-user.latest_active)<datetime.timedelta(minutes=1))

Then False is returned, which does not seem to be done in the query.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
p2mbot, 2016-01-14
@p2mbot

I make all requests over time in the form of:

ObjModel.query.filter(ObjModel.time_field > datetime.now()-timedelta(minutes=xxx))...

Those. in the comparison on the left is the table field. Perhaps this is important for the implementation of the filter method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question