sqlalchemy.exc.InvalidRequestError:

Всех приветствую. Если в кратце, есть следующая структура:

app/models/base_operation.py

class BaseOperation(Base):

    """ Базовый класс для моделей Income и Expense """
    __abstract__ = True

    id: Mapped[int] = mapped_column(Integer, primary_key=True, 
index=True)
    user_id: Mapped[int] = mapped_column(Integer, 
ForeignKey("users.id", ondelete="CASCADE"))
    amount: Mapped[float] = mapped_column(DECIMAL(10, 2), 
nullable=False)
    date: Mapped[str] = mapped_column(Date, nullable=False)
    source: Mapped[str] = mapped_column(String(100), 
nullable=False)
    category: Mapped[str] = mapped_column(String(100), 
nullable=False)
    context: Mapped[str] = mapped_column(Text, nullable=True)

    @declared_attr
    def user(cls):
        return relationship("User", back_populates="operations")

from .user import User  # noqa: E402

app/models/user.py

class User(Base):

    __tablename__ = "users"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, 
index=True)
    username: Mapped[str] = mapped_column(String(50), 
unique=True, nullable=False, index=True)
    email: Mapped[str] = mapped_column(String(100), unique=True, 
nullable=False, index=True)
    password: Mapped[str] = mapped_column(String(20))

    operations: Mapped[list["BaseOperation"]] = 
relationship("BaseOperation",back_populates="user")

from .base_operation import BaseOperation  # noqa: E402

и есть вот такая ошибка:

raise e sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper[User(users)]'. Original exception was: When initializing mapper Mapper[User(users)], expression 'BaseOperation' failed to locate a name ('BaseOperation'). If this is a class name, consider adding this relationship() to the <class 'app.models.user.User'> class after both dependent classes have been defined.

Уже всю голову поломал, пробовал и убирать declared_attr и явно и неявно импорты прописывать, все сводится к этой ошибке. Не понимаю где конкретно ошибка, но понимаю что как будто нехватает или лишняя одна или пара строк. Прошу помощи


Ответы (0 шт):