Why Alembic does not detect models without importing them? #1693
-
Hi all! Imagine there is a base class: models/base.py: from typing import Self
from sqlalchemy import MetaData
from sqlalchemy.orm import DeclarativeBase
metadata = MetaData()
class Model(DeclarativeBase):
metadata = metadata
def update(self, **values) -> Self:
for key, value in values.items():
setattr(self, key, value)
return self and some subclass: models/users.py:
I'd like to use env.py
but if to import some of subclasses of Model then it works:
Why it work in this way? How to make Alembic detect models without importing User? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, That's just how python works, it evaluates modules lazily, the first time you access them. In you case you could add inside the from .base import metadata
from .users import User
... so in alembic env you can then just add |
Beta Was this translation helpful? Give feedback.
Hi,
That's just how python works, it evaluates modules lazily, the first time you access them.
An usual solution to ensure that all models are import is to make use of a module file that imports all models in a single place, so you can import it and you are fine.
__init__
is usually used for this.In you case you could add inside the
models
package and__init__.py
file that has something likeso in alembic env you can then just add
from models import metadata