SQLAlchemy offers tools for database schema generation, querying, and object-relational mapping. Key features include:
SQLAlchemy was first released in February 2006. It has evolved to include a wide range of features for database interaction and has gained popularity among Python developers. Notable versions include:
The following example represents an n-to-1 relationship between movies and their directors. It is shown how user-defined Python classes create corresponding database tables, how instances with relationships are created from either side of the relationship, and finally how the data can be queried — illustrating automatically generated SQL queries for both lazy and eager loading.
Creating two Python classes and corresponding database tables in the DBMS:
One can insert a director-movie relationship via either entity:
SQLAlchemy issues the following query to the DBMS (omitting aliases):
The output:
Setting lazy=True (default) instead, SQLAlchemy would first issue a query to get the list of movies and only when needed (lazy) for each director a query to get the name of the corresponding director:
"0.1 Changelog — SQLAlchemy 2.0 Documentation". docs.sqlalchemy.org. Retrieved 2024-07-04. https://docs.sqlalchemy.org/en/20/changelog/changelog_01.html ↩
"1.0 Changelog — SQLAlchemy 2.0 Documentation". docs.sqlalchemy.org. Retrieved 2024-07-04. https://docs.sqlalchemy.org/en/20/changelog/changelog_10.html#change-1.0.0 ↩
"1.4 Changelog — SQLAlchemy 2.0 Documentation". docs.sqlalchemy.org. Retrieved 2024-07-04. https://docs.sqlalchemy.org/en/20/changelog/changelog_14.html#change-1.4.0 ↩