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
|
# Import all the models, so that Base has them before being
|
||||||
# imported by Alembic
|
# 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 import create_engine
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
# Check if we're running in the container environment
|
# Check if we're running in the container environment
|
||||||
IN_CONTAINER = os.path.exists('/app/repo')
|
IN_CONTAINER = os.path.exists('/app/repo')
|
||||||
|
|
||||||
@ -23,6 +23,4 @@ engine = create_engine(
|
|||||||
SQLALCHEMY_DATABASE_URL,
|
SQLALCHEMY_DATABASE_URL,
|
||||||
connect_args={"check_same_thread": False}
|
connect_args={"check_same_thread": False}
|
||||||
)
|
)
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
Base = declarative_base()
|
|
@ -2,7 +2,7 @@ from datetime import datetime
|
|||||||
from sqlalchemy import Column, Integer, Text, DateTime, ForeignKey
|
from sqlalchemy import Column, Integer, Text, DateTime, ForeignKey
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from app.db.session import Base
|
from app.db.base_class import Base
|
||||||
|
|
||||||
|
|
||||||
class Comment(Base):
|
class Comment(Base):
|
||||||
|
@ -2,7 +2,7 @@ from datetime import datetime
|
|||||||
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean
|
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean
|
||||||
from sqlalchemy.orm import relationship
|
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
|
from app.models.tag import post_tag
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from sqlalchemy import Column, Integer, String, Table, ForeignKey
|
from sqlalchemy import Column, Integer, String, Table, ForeignKey
|
||||||
from sqlalchemy.orm import relationship
|
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
|
# Association table for the many-to-many relationship between posts and tags
|
||||||
post_tag = Table(
|
post_tag = Table(
|
||||||
|
@ -2,7 +2,7 @@ from datetime import datetime
|
|||||||
from sqlalchemy import Boolean, Column, Integer, String, DateTime
|
from sqlalchemy import Boolean, Column, Integer, String, DateTime
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from app.db.session import Base
|
from app.db.base_class import Base
|
||||||
|
|
||||||
|
|
||||||
class User(Base):
|
class User(Base):
|
||||||
|
@ -13,12 +13,18 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Try to import using the container path structure
|
# 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:
|
except ImportError:
|
||||||
# If that fails, try to import using the local path structure
|
# If that fails, try to import using the local path structure
|
||||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
sys.path.insert(0, project_root)
|
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
|
# 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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user