Код: Выделить всё
class TableA(Base):
__tablename__ = "tablea"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
tableb_ref: Mapped[List["TableB"]] = relationship(back_populates="tablea_ref", secondary="tablec")
Код: Выделить всё
class TableB(Base):
__tablename__ = 'tableb'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(50), nullable=False)
tablea_ref: Mapped[List["TableA"]] = relationship(back_populates="tableb_ref", secondary="tablec")
Код: Выделить всё
class TableC(Base):
__tablename__ = "tablec"
tablea_id: Mapped[int] = mapped_column(ForeignKey('tablea.id', ondelete='CASCADE'), primary_key=True)
tableb_id: Mapped[int] = mapped_column(ForeignKey('tableb.id', ondelete='CASCADE'), primary_key=True)
Код: Выделить всё
sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper[TableA(tablea)], expression 'tablec' failed to locate a name ("name 'tablec' is not defined"). If this is a class name, consider adding this relationship() to the class after both dependent classes have been defined.
вот как я пытаюсь это использовать (еще в одном файле с импортом только TableA):
Код: Выделить всё
async def get_all():
async with session_factory() as session:
stmt = select(TableA)
res = await session.execute(stmt)
tables = res.scalars().all()
return tables
Подробнее здесь: https://stackoverflow.com/questions/798 ... rent-files
Мобильная версия