
Introduce SQLAlchemy 2.0 and Alembic for database building and migrations, explore synchronous and asynchronous connections, and integrate with a telegram bot using Gram across Postgres and other backends.
Learn to set up a Python 3.10 environment with a virtual environment in PyCharm, install PostgreSQL via Docker or locally, and configure SQLAlchemy with psycopg2 and its binary.
Set up a PostgreSQL database in a Docker container using the PostgreSQL 13.4 Alpine image, configuring environment variables for user, password, and database, and verify the instance is ready.
Establish a PostgreSQL connection with SQLAlchemy by creating an engine, building a URL, and using a session pool to execute text queries and manage transactions.
Execute and fetch data with SQLAlchemy by creating a sample telegram users table, inserting records, and retrieving results with cursor, all, and scalar methods; compare textual SQL and ORM usage.
Learn to create tables with SQLAlchemy's ORM by mapping Python classes to database tables using a declarative base, mapped columns, primary keys, constraints, and Alembic migrations.
Learn to define a user table by mapping a Python class to SQLAlchemy columns, specifying mapped types, nullable constraints, created at timestamp with server default, and a self-referencing foreign key.
Explains mixins in SQLAlchemy to reuse column definitions, including a TimestampMaxin with createdAt and updatedAt, and uses declaredAttributeDirective to auto name tables and prevent naming mistakes.
Learn how to reuse column declarations with annotated types and mapped columns in SQLAlchemy, and model primary keys, foreign keys, and many-to-many relations for orders and products.
Learn to create tables with SQLAlchemy using a declarative base and metadata, configure an engine, use create_all and drop_all, and prepare for Alembic migrations.
Master database migrations with lambic: install, configure an environment, import models to align base metadata, and generate offline or online migrations that alter tables and data using SQLAlchemy.
Learn how to create your first Alembic migration for a SQLAlchemy project by configuring metadata, using auto generate, and applying upgrade head to tables such as products, users, and orders.
Learn how to modify a database using Alembic migrations, adjusting column lengths, adding and renaming columns, and applying upgrades to reflect product, language, and price schemas.
Learn to insert data into a users table using the SQLAlchemy ORM, via a repository and session, with insert statements, handling nulls, and committing.
Learn advanced select queries with SQLAlchemy ORM, including get user by telegram id, selecting specific columns, using where clauses, scalars, and ordering and limiting results.
Learn how to combine insert, select, and update in a single Postgres query using on conflict do nothing or update, returning the full user record and committing the transaction.
Seed initial data in tables using Faker to generate users, orders, and products, including referrer IDs, and save results for testing queries, then migrate with Alembic.
Establish one-to-many and bidirectional relationships in SQLAlchemy by mapping parent and child tables, using back_populates and foreign keys, and configure many-to-many via an association table for orders, products, and users.
Explore inner and outer joins on a self-referencing user table, using an alias to distinguish parent and referral users, and apply on clause to fetch invited users.
Learn to join users, orders, and products using SQLAlchemy ORM relationships, leveraging the orders products association table to run a single efficient joined query and access specific fields.
Master aggregated queries in sqlalchemy, using count, sum, average, min, and max; join users and orders, apply group by, labeling, and having for precise insights.
Update queries with ORM demonstrate updating a user row using SQLAlchemy's update function, applying a where clause, setting new values via values, and committing, with referer constraints highlighted.
Delete records with an ORM by specifying a where clause on the userTelegramId and committing the transaction to persist changes.
Learn how to perform bulk inserts with orm using bindParam in SQLAlchemy to add multiple products to an order by inserting productId and quantity from dictionaries, then commit.
Learn to migrate SQLAlchemy to asynchronous mode by using an async engine, adapting sessions, and enabling async Alembic migrations for apps like Telegram bots and FastAPI.
This comprehensive course is designed for users who have some knowledge of SQL, and it aims to teach you how to leverage the power of SQLAlchemy and Alembic to streamline your work with databases. By learning the techniques taught in this course, you will have a better understanding of Object Relational Mapping (ORM), how to create and manipulate database tables using Python, and how to manage and track changes using Alembic migrations.
Throughout the course, you will gain hands-on experience by working on various practical examples and exercises. The course will be easy to follow, with a focus on making complex tasks easier to understand and apply in your own projects.
Key topics include:
1. Introduction to SQLAlchemy and Alembic
- Overview of SQLAlchemy and its benefits
- Getting started with Alembic for database migrations
2. Creating tables with SQLAlchemy
- Declarative base and table class creation
- Working with different column types and constraints
- Using Mixin classes to reuse column and key definitions
3. Working with data using ORM
- Inserting, updating, and deleting rows
- Querying data and using filtering options
- Working with relationships and joins.
4. Handling schema changes and migrations with Alembic
- Setting up an Alembic environment and configuration
- Generating, reviewing, and customizing migration scripts
- Upgrading and downgrading database schemas
5. Advanced SQLAlchemy features
- Query optimizations
- Building complex queries
- Managing transactions and session handling
6. Best practices and real-world case examples
- Implementing reusable patterns
- Scaling and performance considerations
- Common SQLAlchemy pitfalls and solutions
Upon completion of this course, you will have a strong foundation in using SQLAlchemy and Alembic to manage your databases effectively. You will be equipped with the skills necessary to create and manipulate databases with ease, enabling you to bring value to your projects and organization.