How to get Alembic to recognise SQLModel database model?
Asked Answered
C

2

22

Using SQLModel how to get alembic to recognise the below model?

from sqlmodel import Field, SQLModel

class Hero(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    name: str
    secret_name: str
    age: Optional[int] = None

One approach I've been looking at is to import the SQLalchemy model for Alembic but looking through the source code I can't find how to do that.

How to make Alembic work with SQLModel models?

Corvette answered 26/8, 2021 at 3:21 Comment(3)
What do you mean by "get alembic to recognise the below model"? I tried to run migrations with alembic a couple of days ago and changes were recognisableAltamirano
Anyway I can try to spell out detail how I got it all working.Altamirano
If you got the changes to be recognisable that's all I was after, can you post your solution?Corvette
A
36

There should be info about that in Advanced user guide soon with better explanation than mine but here is how I made Alimbic migrations work.

First of all run alembic init migrations in your console to generate migrations folder. Inside migrations folder should be empty versions subfolder,env.py file, script.py.mako file. In script.py.mako file we should add line import sqlmodel somewhere around these two lines

#script.py.mako
from alembic import op
import sqlalchemy as sa
import sqlmodel # added

Then we should edit env.py file

#env.py
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

from app.models import * # necessarily to import something from file where your models are stored

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None 
# comment line above and instead of that write
target_metadata = SQLModel.metadata

While writing came up with an idea that you forgot to import something from your models.py (or anywhere else your models are stored). And that was the main problem

Also, an important note would be saving changes in your models by pressing ctrl(CMD) + S - there are some issues with that.

Finally,running

 alembic revision --autogenerate -m "your message"

should generate a new .py file in versions folder with your changes. And

 alembic upgrade head  

Applies your changes to DB.

Altamirano answered 5/9, 2021 at 13:30 Comment(5)
As mentioned at TestDriven.io Post too.Carbineer
Three is a PR for this here, you can look in the comments and see the instructions there too: github.com/tiangolo/sqlmodel/pull/512Cinelli
It would be better to use from app import models instead of from app.models import * in env.pyEtiolate
When you add target_metadata = SQLModel.metadata to your env.py, did you need to also add the from sqlmodel import SQLModel there? In my case when I ran alembic revisons --autogenerate -m "your message" it gave the error: "NameError: name 'SQLModel' is not defined".Apocarpous
yes, from sqlmodel import SQLModel has to be added tooSuffuse
M
1

Here you can find a fastapi-alembic and SQLmodel integration with async PostgreSQL database https://github.com/jonra1993/fastapi-sqlmodel-alembic

Mahayana answered 27/2, 2022 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.