Skip to content

Commit 61d84fa

Browse files
authored
Merge pull request #167 from geekashu/main
Dynamically importing app models
2 parents 9fe4303 + 6ea2127 commit 61d84fa

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,6 @@ cython_debug/
164164
# Macos
165165
.DS_Store
166166

167-
.ruff_cache
167+
.ruff_cache
168+
169+
.idea

src/migrations/env.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import asyncio
2+
import importlib
3+
import pkgutil
24
from logging.config import fileConfig
35

46
from alembic import context
@@ -23,9 +25,14 @@
2325
fileConfig(config.config_file_name)
2426

2527
# add your model's MetaData object here
26-
# for 'autogenerate' support
27-
# from myapp import mymodel
28-
# target_metadata = mymodel.Base.metadata
28+
# Auto-import all models in app.models
29+
def import_models(package_name):
30+
package = importlib.import_module(package_name)
31+
for _, module_name, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
32+
importlib.import_module(module_name)
33+
34+
# Load all models dynamically
35+
import_models("app.models")
2936
target_metadata = Base.metadata
3037

3138
# other values from the config, defined by the needs of env.py,

0 commit comments

Comments
 (0)