Fix Alembic migration import error
- Updated env.py to correctly handle Python path by adding project root to sys.path - Added proper imports with noqa annotations to handle linting exceptions - Added prepend_sys_path setting to alembic.ini for better path resolution - Fixed import of models to ensure they're properly detected by Alembic
This commit is contained in:
parent
2bc28761f5
commit
9bfdefc0b4
@ -4,6 +4,9 @@
|
|||||||
# path to migration scripts
|
# path to migration scripts
|
||||||
script_location = app/db/migrations
|
script_location = app/db/migrations
|
||||||
|
|
||||||
|
# prepend current directory to path (helps with imports)
|
||||||
|
prepend_sys_path = .
|
||||||
|
|
||||||
# template used to generate migration files
|
# template used to generate migration files
|
||||||
# file_template = %%(rev)s_%%(slug)s
|
# file_template = %%(rev)s_%%(slug)s
|
||||||
|
|
||||||
|
@ -1,10 +1,21 @@
|
|||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config
|
from sqlalchemy import engine_from_config
|
||||||
from sqlalchemy import pool
|
from sqlalchemy import pool
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
from app.db.base import Base
|
|
||||||
|
# Add the parent directory to sys.path to allow imports from the app package
|
||||||
|
# Determine the parent directory of the migrations directory
|
||||||
|
current_path = Path(__file__).parent.parent.parent.parent
|
||||||
|
sys.path.insert(0, str(current_path))
|
||||||
|
|
||||||
|
# Import Base after setting up sys.path
|
||||||
|
from app.db.base import Base # noqa: E402
|
||||||
|
# Import all models to register them with SQLAlchemy metadata
|
||||||
|
from app.models.file import File # noqa: E402, F401 - import needed for Alembic to detect models
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
@ -16,8 +27,6 @@ fileConfig(config.config_file_name)
|
|||||||
|
|
||||||
# add your model's MetaData object here
|
# add your model's MetaData object here
|
||||||
# for 'autogenerate' support
|
# for 'autogenerate' support
|
||||||
# from myapp import mymodel
|
|
||||||
# target_metadata = mymodel.Base.metadata
|
|
||||||
target_metadata = Base.metadata
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
# other values from the config, defined by the needs of env.py,
|
# other values from the config, defined by the needs of env.py,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user