I need to build tables like in the picture, it turns out to be a very complex structure where I understand what relationships need to be used, but I don’t understand how to do it
example pic
class ChampionshipsTable(Base):
__tablename__ = "championships"
id: Mapped[int] = mapped_column(primary_key=True)
CId: Mapped[int] = mapped_column(Integer(), unique=True)
championship_name = mapped_column(String(50))
class TournamentsTable(Base):
__tablename__ = "tournaments"
id: Mapped[int] = mapped_column(primary_key=True)
CId: Mapped[int] = mapped_column(ForeignKey("championships.CId"))
TId: Mapped[int] = mapped_column(Integer(), unique=True)
tournament_name: Mapped[str] = mapped_column(String(50))
class MatchesTable(Base):
__tablename__ = "matches"
id: Mapped[int] = mapped_column(primary_key=True)
CId: Mapped[int] = mapped_column(ForeignKey("championships.CId"))
TId: Mapped[int] = mapped_column(ForeignKey("tournaments.TId"))
match_id: Mapped[int] = mapped_column(Integer(), unique=True)
Is there any universal solution?
I try to build tables like in many-to-many example in sqlalchemy like that:
class ChampionshipsTable(Base):
__tablename__ = "championships"
id: Mapped[int] = mapped_column(primary_key=True)
CId: Mapped[int] = relationship("TournamentsTable", secondary="matches", back_populates="championships")
championship_name = mapped_column(String(50))
class TournamentsTable(Base):
__tablename__ = "tournaments"
id: Mapped[int] = mapped_column(primary_key=True)
CId: Mapped[int] = relationship("ChampionshipsTable", back_populates="tournaments")
TId: Mapped[int] = relationship("MatchesTable", backref="tournaments")
tournament_name: Mapped[str] = mapped_column(String(50))
class MatchesTable(Base):
__tablename__ = "matches"
id: Mapped[int] = mapped_column(primary_key=True)
CId: Mapped[int] = mapped_column(ForeignKey("championships.id"))
TId: Mapped[int] = mapped_column(ForeignKey("tournaments.id"))
match_id: Mapped[int] = mapped_column(Integer(), unique=True)
and thats what i got: sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper[TournamentsTable(tournaments)]' has no property 'championships'. If this property was indicated from other mappers or configure events, ensure registry.configure() has been called.
I dont know what to do