Skip to main content

Posts

Showing posts with the label alembic

SQLAlchemy and Alembic

  SQLAlchemy and Alembic: Explained with Example SQLAlchemy: A powerful Python library for interacting with relational databases. Provides an object-relational mapper (ORM) that lets you define your data model as Python classes and map them to tables in a database. Simplifies writing SQL queries and manipulating data through its object-oriented interface. Alembic: A migration tool built on top of SQLAlchemy. Allows you to track changes to your database schema over time and manage upgrades and downgrades. Generates migration scripts as your data model evolves, providing version control for your database schema. Example: Let's consider a model that defines a User table with two attributes: id (primary key) and username . Python code (SQLAlchemy): Python from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class User ( Base ): __tablename__ = "users" id = Column...