I have a simple many-to-many relationship with associated table:
with following data:
ONE user can play MANY matches and
ONE match can involve up to TWO users
I want to realize proper relationships in both "Match" and "User" classes
users_matches_table = Table('users_matches', Base.metadata, Column('match_id', Integer, ForeignKey('matches.id', onupdate="CASCADE", ondelete="CASCADE")), Column('user_id', Integer, ForeignKey('users.id', onupdate="CASCADE", ondelete="CASCADE")) ) class Match(Base): __tablename__ = 'matches' id = Column(Integer, primary_key=True) #relations user1 = relationship('User', secondary = users_matches_table) user2 = relationship('User', secondary = users_matches_table) class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) username = Column(String) #relations matches = relationship('Match', secondary=users_matches_table)
But obviously, there should be some rules for Match.user1 and Match.user2 relationships, that will differ one User from another, so i could get
match1.user2 and they won't reference to the same User. Any ideas how to do that?