Код: Выделить всё
SELECT *
FROM user_sports us
JOIN sports s ON s.id = us.sport_id
AND s.sport = 'baseball'
JOIN baseball b ON b.user_sport_id = us.id
Код: Выделить всё
class UserSports(Base):
__tablename__ = "user_sports"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(nullable=False)
sport_id: Mapped[int] = mapped_column(ForeignKey('states.id', nullable=False)
sports: Mapped['Sports'] = relationship(back_populates='user_sports', foreign_keys=[sport_id])
baseball: Mapped['Baseball'] = relationship(back_populates='user_sports', primaryjoin='and_(UserSports.id == Baseball.user_sport_id, UserSports.sport_id.has(sport='baseball'))'
class Baseball(Base):
__tablename__ = "baseball"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped(column(ForeignKey('users.id', nullable=False)
position: Mapped[str] = mapped_column(TEXT, nullable=False)
team: Mapped[str] = mapped_column(TEXT, nullable=False)
number: Mapped[int] = mapped_column(nullable=False)
user_sports: Mapped['UserSports'] = relationship(back_populates='baseball', foreign_key='user_id')
class Sports(Base):
__tablename__ = "sports"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
sport: Mapped[str] = mapped_column(TEXT, nullable=False)
user_sports: Mappped[List["UserSports"]] = relationship(back_populates='sports', foreign_key='UserSports.sport_id')
Код: Выделить всё
Property 'sport_id' is not an instance of ColumnProperty (i.e. does not correspond directly to a Column).
Подробнее здесь: https://stackoverflow.com/questions/788 ... er-table-i