Print Raw SQL query from SQLAlchemy Session Query Method
Jul 5, 2024
Imagine you have an SQLAlchemy ORM query to print all the Items in database
class Item(Base):
__tablename__ = "item"
id = mapped_column(Integer, primary_key=True)
name = mapped_column(String)
category = mapped_column(String)
the query method will look like
result = db_session.query(Item)
Now lets print the actual SQL Query which is run behind the scenes
print(str(result))
And you would get the output
