Is there a way I can print a query created by sqlalchemy
emp = session.query(Employee).filter(*empfilters).order_by(Employee.Name).all()
I've tried doing str(emp)
, emp.statement.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})
or other solutions from SO, but couldn't reproduce anything.
The *empfilters
again here comes from other tables (part of one-many join) so I wanted to check what exactly it's being compiled to.
The error if I try to do it by above is - 'list' object has no attribute 'statement'
order_by(Employee.Name)
. When you use.all()
at the end, the query runs and returns a list, hence your error message. If you remove.all()
you should be able to examine the query string.global name 'postgresql' is not defined
-- Do I need to add it @ac24?str(emp)
should let you view the query in native sql. If you are having other issues please include more code to demonstrateempfilters
etc.all()
and then doingprint emp