From 11ef92d44e0bebe36b3928c04fb9d41ce210ae45 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Mon, 2 Jun 2025 22:08:10 +0000 Subject: [PATCH] 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 --- app/db/base.py | 7 +++++++ app/db/base_class.py | 3 +++ app/db/session.py | 6 ++---- app/models/comment.py | 2 +- app/models/post.py | 2 +- app/models/tag.py | 2 +- app/models/user.py | 2 +- migrations/env.py | 10 ++++++++-- 8 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 app/db/base_class.py diff --git a/app/db/base.py b/app/db/base.py index 990c3e4..d5b3344 100644 --- a/app/db/base.py +++ b/app/db/base.py @@ -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 diff --git a/app/db/base_class.py b/app/db/base_class.py new file mode 100644 index 0000000..7c2377a --- /dev/null +++ b/app/db/base_class.py @@ -0,0 +1,3 @@ +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() \ No newline at end of file diff --git a/app/db/session.py b/app/db/session.py index 48c9d96..6e1831c 100644 --- a/app/db/session.py +++ b/app/db/session.py @@ -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() \ No newline at end of file +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) \ No newline at end of file diff --git a/app/models/comment.py b/app/models/comment.py index 161f13c..d067125 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -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): diff --git a/app/models/post.py b/app/models/post.py index 00ba289..dabf3a3 100644 --- a/app/models/post.py +++ b/app/models/post.py @@ -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 diff --git a/app/models/tag.py b/app/models/tag.py index 194f7b2..139fdf7 100644 --- a/app/models/tag.py +++ b/app/models/tag.py @@ -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( diff --git a/app/models/user.py b/app/models/user.py index 6a9d7d9..6c6086e 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -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): diff --git a/migrations/env.py b/migrations/env.py index b27a705..bad92b8 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -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.