Код: Выделить всё
# ./models/user.py
class UserModel(Base):
__tablename__ = 'users'
id: Mapped[int] = mapped_column(primary_key=True)
characters: Mapped[list['CharacterModel']] = relationship(back_populates='user')
# ./models/character.py
class CharacterModel(Base):
__tablename__ = 'characters'
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id', ondelete='CASCADE'))
characters: Mapped['UserModel'] = relationship(back_populates='characters')
# ./models/__init__.py
from . import user, character
Код: Выделить всё
sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper[UserModel(users)], expression 'CharacterModel' failed to locate a name ('CharacterModel'). If this is a class name, consider adding this relationship() to the class after both dependent classes have been defined.
Подробнее здесь: https://stackoverflow.com/questions/789 ... rate-files