When building a Python application, I needed to fetch data spread across two related tables in a single query. This is called performing a join in SQL.
Here is what I learned about doing joins with SQLAlchemy.
The SQL
I knew the SQL I needed to execute.
|
|
But how does it work with SQLAlchemy?
The query using Python
Write the SQLAlchemy statement in the following way:
|
|
I’d like to add a few comments:
session.query(EventEntity, EventJournalEntity)tells SQLAlchemy you want columns from both tables—the result will be a list of tuples rather than a flat list of one entity..join(EventJournalEntity, EventEntity.event_id == EventJournalEntity.event_id)specifies the target table and theONcondition explicitly, which avoids ambiguity when the relationship isn’t preconfigured on the model.- You can chain
.filter(),.order_by(), and other clauses after.join()as you normally would on a single-entity query.
I’ll probably go into other more complex scenarii when I run into them.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Photo by Kindel Media.