ORM

Even though SQL expression language already provides a good level of abstraction, it still operates with a physical data model, not with business entities.

ORM makes it possible to map business entities, that a Python application deals with, to data structures in the database. To show this, let's create a Car class and define the mapping.

When using ORM, we describe the database structure and, at the same time, define the classes that represent business entities. This is done using a system named declarative. In SQLAlchemy, this system is initiated by creating a class that is then used as a base class for other classes:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

Now the high-level class that will ...

Get Learning PostgreSQL 11 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.