I'm trying to perform the following query in Sqlalchemy.
Select * from "Mytable" where Date(date_time_field) = "2011-08-16";
I have tried several methods. Some here on SO. But none seems "realistic" since some do Casts Other String Formating ?!?! and not a plain simple Date() ( By Date i Mean the Postgresql Date not the Python One) at the ORM Field.
Is it possible at the ORM level to declare a Simple Date() around the queried field ?
Best Regards António
This SQLAlchemy engine is a global object which can be created and configured once and use the same engine object multiple times for different operations. The first step in establishing a connection with the PostgreSQL database is creating an engine object using the create_engine() function of SQLAlchemy.
The second one, filter_by(), may be used only for filtering by something specifically stated - a string or some number value. So it's usable only for category filtering, not for expression filtering. On the other hand filter() allows using comparison expressions (==, <, >, etc.)
first() applies a limit of one within the generated SQL, so that only one primary entity row is generated on the server side (note this may consist of multiple result rows if join-loaded collections are present). Calling Query. first() results in an execution of the underlying query.
Using @Ants Aasma Comment.
And to Keep it clean for any web search.
from sqlalchemy import Date, cast from datetime import date my_data = session.query(MyObject).\ filter(cast(MyObject.date_time,Date) == date.today()).all()
Thank you all who tried to solve this problem :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With