
PYTHON — Sqlalchemy Orm In Python
Technology is best when it brings people together. — Matt Mullenweg
Insights in this article were refined using prompt engineering methods.

PYTHON — Implementing Stack In Python
# Understanding SQLAlchemy ORM in Python
In this tutorial, we’ll cover the basics of SQLAlchemy’s Object-Relational Mapping (ORM) in Python. SQLAlchemy is a powerful library for working with databases in Python. It provides a high-level interface for creating and executing database queries using Python objects, which map to the tables in a database.
What is ORM?
Object-Relational Mapping (ORM) is an abstraction that allows Python objects to map to tables in a database. This means that when you perform queries, you work with objects instead of raw database records, and the properties of these objects reflect the columns in the corresponding tables.
Declaring ORM Objects
The key to using ORM is declaring Python objects that map to database tables. In SQLAlchemy, you can achieve this by creating a class that inherits from declarative_base(), a factory provided by SQLAlchemy. Each class should specify the name of the table it corresponds to using the __tablename__ special attribute, and then declare the attributes that map to the columns in the table using the Column object.
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
class Author(Base):
__tablename__ = 'author'
author_id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
books = relationship("Book", back_populates="author")
class Book(Base):
__tablename__ = 'book'
book_id = Column(Integer, primary_key=True)
title = Column(String)
author_id = Column(Integer, ForeignKey('author.author_id'), nullable=False)
author = relationship("Author", back_populates="books")In the example above, we’ve defined two classes: Author and Book, each representing a table in the database. The relationship() function is used to establish the relationship between the two classes, representing the foreign key relationship between the tables.
Performing Queries with ORM
Once the ORM objects are declared, you can use them to perform queries. Instead of using raw SQL or database-specific query languages, you can write Python code that operates on the ORM objects.
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# Create an engine
engine = create_engine('sqlite:///mydatabase.db')
# Create a session
Session = sessionmaker(bind=engine)
session = Session()
# Query all authors
authors = session.query(Author).all()
for author in authors:
print(author.first_name, author.last_name)
# Query books using a join
books = session.query(Book).join(Book.author).filter(Author.last_name > "B").all()
for book in books:
print(book.title)In the example above, we first create an engine to connect to the database and then create a session to interact with the database. We then perform queries using the ORM objects and their relationships.
Conclusion
In this tutorial, we’ve covered the basics of SQLAlchemy’s Object-Relational Mapping (ORM) in Python. We’ve seen how to declare ORM objects that map to database tables, and how to perform queries using these objects. SQLAlchemy’s ORM provides a powerful and intuitive way to interact with databases in Python.
Have fun experimenting with SQLAlchemy ORM in your Python projects!






