Загрузка...

FastOpp Country Store 2.4 – Create SQLModel Class and Alembic Migration (SQLite)

FastOpp Country Store steps into real database design in this Module 2 lesson by creating a dedicated SQLModel class for food descriptions and wiring it to SQLite with Alembic migrations. Create SQLModel Python class in FastAPI application and translate to SQLite database with SQLAlchemy and Alembic using FastOpp wrapper. This project is aimed at students and newer developers and is not intended for production use, but the workflow mirrors how a real FastAPI + SQLModel app evolves as requirements grow.

This video, “FastOpp Country Store 2.4 – SQLModel FoodDescription and Alembic Migration (SQLite)”, is part of FastOpp Country Store – Module 2: Intermediate FastAPI Services and AI Drafts. You take a single-table foods setup and introduce a separate FoodDescription model to support multiple versions and an approval workflow in later videos.

In this video, you will:

* Create a new FoodDescription SQLModel class in models.py by copying and adapting the existing Food model
* Map the new model to its own table and add a foreign key back to the foods table
* Generate and apply an Alembic migration using the FastOpp oppman.py wrapper
* Add an admin view for FoodDescription so you can inspect records in the web UI
* Extend the model with versioning, status, published flags, and audit fields
* Use the sqlite3 CLI to drop a broken table and rerun migrations when SQLite cannot alter it in place
* Push your updated schema and code to your own GitHub repository

What you build step by step:

* Define the FoodDescription model:

* Copy the Food model, rename the class and table, and keep the UUID primary key
* Remove fields that are not needed and add a foreign key to link each description back to a food record

* Generate and run the migration:

* Use uv run python oppman.py migrate create to generate a new Alembic migration
* Apply the migration with uv run python oppman.py migrate upgrade
* If the migration fails because of SQLite limitations, fix the schema and regenerate

* Register FoodDescription in the admin:

* Copy the Food admin view to create a FoodDescription admin view
* Import and register it with admin.add_view(...)
* Restart the server and confirm you can see FoodDescription in the admin panel

* Add workflow and tracking fields:

* Add an integer version field so each food item can have multiple description versions
* Add a status field (string) with a default of "draft"
* Add a published boolean flag to mark which version is live
* Add created_at, updated_at, approved_at, and published_at datetime fields
* Add fields to track who created and who approved the description
* Add an optional notes field for reviewer or author comments

* Work around SQLite alter-table limitations:

* Use sqlite3 to open the test database and drop the old food_description table
* Rerun the migration so Alembic can create the correct table from scratch
* Confirm all fields appear correctly in the admin panel

This video is one part of FastOpp Country Store – Module 2: Intermediate FastAPI Services and AI Drafts, which builds on the beginner Country Store app by adding drafts, review queues, and human approval around LLM-generated content. The code here remains a learning project, but the way you design models and migrations is directly transferable to more serious FastAPI + SQLModel applications.

Watch the full beginner course (Module 1):
https://www.youtube.com/playlist?list=PLJVZE-yg6ykADrM4qb0WHJAp37--rIkCw

Watch the full intermediate course (Module 2):
https://www.youtube.com/playlist?list=PLJVZE-yg6ykB4X-81bQGxrBEbznHr3H8y

GitHub repo (FastOpp code):
https://github.com/Oppkey/fastopp

Who this is for:

* Students and newer Python developers learning how models, migrations, and admin views fit together
* FastAPI and SQLModel users who want a concrete, SQLite-friendly pattern for evolving schemas
* Anyone practicing how to combine SQLModel, SQLAlchemy, Alembic, and FastOpp oppman.py commands in a safe, non-production project

By the end of this lesson, you will have a FoodDescription model with versioning, workflow, and audit fields defined in SQLModel, migrated into SQLite with Alembic, exposed in the admin, and committed and pushed to your GitHub repo, ready for the service-layer logic in the next video.

#fastapi #python #sqlmodel #sqlalchemy #alembic #sqlite #ai #webdevelopment #fastoppproject #fastopp

Видео FastOpp Country Store 2.4 – Create SQLModel Class and Alembic Migration (SQLite) канала Oppkey
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять