Print Raw SQL query from SQLAlchemy Session Query Method

tech kamar
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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations