SQLAlchemy terms defined
Posted on February 20, 2018 • 2 minutes • 246 words
In January of last year I was working on a project that used SQLAlchemy to manage database interactions. Since many of the terms were new to me back then, I thought it would be handy to define a few of them here.
ORM - Object-Relational Mapping
Provides an intermediary between the object-oriented code and the SQL statements. In practical terms, this allows us to manipulate our objects, and the corresponding SQL command to replicate the actions we’ve taken will be generated by the ORM, which in this instance is SQLAlchemy.
Engine
The object which manages connections to the database. It is used to control and manage the database’s resources and DBAPI connections. Usually, there’s only one per application.
Session
Represents a series of transactions with the database. Holds all objects which have yet to be written to the database. The session is a staging area where objects can be added and manipulated before pushing the session to the database as a series of SQL statements emitted by the ORM.
Sessionmaker
Factory object to create sessions. Creates new sessions as needed once instantiated along with the engine. Usually, there’s only one per application.
Transactions and Object States
Detached
An object in the database, but not part of any session.
Transient
Created within the session, but not yet saved to the database.
Pending
An object that has been added to the session using the add() method.
Persistent
An object in the session that is saved in the database.