Fix circular import issue in database models
- Created a dedicated base_class.py file to define the SQLAlchemy Base - Updated all models to import Base from base_class.py instead of session.py - Modified migrations/env.py to properly import models and Base - Fixed circular dependency between models and base classes
This commit is contained in:
parent
cf63bb6a60
commit
11ef92d44e
@ -1,2 +1,9 @@
|
||||
# Import all the models, so that Base has them before being
|
||||
# imported by Alembic
|
||||
from app.db.base_class import Base # noqa
|
||||
|
||||
# Import all the models to ensure they are registered with SQLAlchemy
|
||||
from app.models.user import User # noqa
|
||||
from app.models.post import Post # noqa
|
||||
from app.models.comment import Comment # noqa
|
||||
from app.models.tag import Tag, post_tag # noqa
|
||||
|
3
app/db/base_class.py
Normal file
3
app/db/base_class.py
Normal file
@ -0,0 +1,3 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
Base = declarative_base()
|
@ -1,9 +1,9 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
|
||||
# Check if we're running in the container environment
|
||||
IN_CONTAINER = os.path.exists('/app/repo')
|
||||
|
||||
@ -23,6 +23,4 @@ engine = create_engine(
|
||||
SQLALCHEMY_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False}
|
||||
)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
@ -2,7 +2,7 @@ from datetime import datetime
|
||||
from sqlalchemy import Column, Integer, Text, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.db.session import Base
|
||||
from app.db.base_class import Base
|
||||
|
||||
|
||||
class Comment(Base):
|
||||
|
@ -2,7 +2,7 @@ from datetime import datetime
|
||||
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.db.session import Base
|
||||
from app.db.base_class import Base
|
||||
from app.models.tag import post_tag
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
from sqlalchemy import Column, Integer, String, Table, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.db.session import Base
|
||||
from app.db.base_class import Base
|
||||
|
||||
# Association table for the many-to-many relationship between posts and tags
|
||||
post_tag = Table(
|
||||
|
@ -2,7 +2,7 @@ from datetime import datetime
|
||||
from sqlalchemy import Boolean, Column, Integer, String, DateTime
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.db.session import Base
|
||||
from app.db.base_class import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
|
@ -13,12 +13,18 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
try:
|
||||
# Try to import using the container path structure
|
||||
from app.db.base import Base
|
||||
# First, import the Base class
|
||||
from app.db.base_class import Base
|
||||
# Then import the models to ensure they're registered with SQLAlchemy
|
||||
import app.db.base # noqa: F401
|
||||
except ImportError:
|
||||
# If that fails, try to import using the local path structure
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, project_root)
|
||||
from app.db.base import Base
|
||||
# First, import the Base class
|
||||
from app.db.base_class import Base
|
||||
# Then import the models to ensure they're registered with SQLAlchemy
|
||||
import app.db.base # noqa: F401
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
|
Loading…
x
Reference in New Issue
Block a user