Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In enterprise software engineering, connecting object-oriented application code with relational database schemas is a fundamental challenge. SQLAlchemy is the standard Object-Relational Mapping (ORM) toolkit for Python.
With the release of SQLAlchemy 2.0, the library unified its Core and ORM APIs, introduced first-class support for Python type annotations (Mapped, mapped_column), and transitioned to a declarative SQL-like querying paradigm.
SQLAlchemy maps Python classes to relational database tables. In 2.0, classes inherit from DeclarativeBase and define column attributes using type-annotated descriptors:
Three primary architectural constructs manage database operations:
Engine: The low-level connection pool and SQL dialect translator. Created via create_engine().Session: The Unit of Work and Identity Map pattern coordinator. It tracks changes to objects and flushes transactions.DeclarativeBase: The root class collecting schema metadata and table definitions.User(...)); not associated with any session; has no database identity.session.add(u)); not yet flushed to the database.How data and code flow step-by-step
select() and session.scalars()In legacy SQLAlchemy 1.x, querying relied on session.query(User).filter(...).
In SQLAlchemy 2.0, all queries use explicit select() statements:
session.execute(select(User)): Returns a Result containing row tuples (User,).session.scalars(select(User)): Automatically unwraps single-entity rows into ScalarResult containing User instances directly.| Construct | Role | 2.0 Syntax |
|---|---|---|
| Model Base | Defines root declarative metadata | class Base(DeclarativeBase): pass |
| Typed Columns | Declares columns with Python types | col: Mapped[type] = mapped_column(...) |
| Engine | Connection pool & dialect gateway | create_engine("dialect://user:pass@host/db") |
| Session | Unit of Work coordinator | with Session(engine) as session: |
| Querying | Declarative SQL queries | session.scalars(select(Model).where(...)) |
SQLAlchemy 2.0 deprecates session.query(Model) in favor of declarative select(Model) executed via session.scalars(select(Model)).
Always manage database sessions with context managers (with Session(engine) as session:) to guarantee sessions and connection pool handles are released back to the pool.
Define a modern SQLAlchemy 2.0 Product model using Mapped and mapped_column, insert records, and query with select().
How are table columns defined with strict type-safety in modern SQLAlchemy 2.0? A. col = Column(Integer) B. col: Mapped[int] = mapped_column(...) C. col = Field(int) D. col = db.Integer()
Mapped[T] and mapped_column(...) to integrate directly with Python's typing system (PEP 484) and static type checkers like Mypy.What is the difference between session.execute(select(User)) and session.scalars(select(User))? A. execute() only works for inserts, while scalars() works for selects. B. execute() returns rows of tuples (User,), whereas scalars() unwraps the first column of each row into raw scalar ORM instances. C. scalars() does not support where clauses. D. execute() bypasses the database engine.
session.scalars() is a convenience method that automatically extracts the first element from each row tuple, yielding ORM entity instances directly.What is the state of a newly created ORM object user = User(name="Alex") before session.add(user) is executed? A. Persistent B. Pending C. Transient D. Detached
What does Base.metadata.create_all(engine) do? A. Deletes all data from the database. B. Inspects all mapped models registered under Base and issues CREATE TABLE DDL statements for any tables that do not yet exist in the database. C. Compiles Python code to SQLite binaries. D. Drops the database connection pool.
create_all() examines the metadata dictionary collected by DeclarativeBase and generates the corresponding schema tables in the target database if they are missing.Why should SQLAlchemy sessions always be managed using a with Session(engine) as session: context manager block? A. Because Python refuses to compile sessions outside of with blocks. B. To guarantee that database connections are properly closed, pooled, and cleaned up upon exit, avoiding connection leaks. C. To turn on SQLite WAL mode. D. To disable transaction isolation.
In the next lesson, we will continue your advanced Python journey with Relationships in Databases (9: Advanced Database Handling).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Project: Async Web Scraper | Relationships in Databases |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.