I started off with this setup:
checklist_participants = Table('checklist_participants', base.metadata,
                               Column('checklist_id', Integer, ForeignKey('checklists.id', ondelete='cascade')),
                               Column('user_id', Integer, ForeignKey('users.id', ondelete='cascade'))
                               )
class Checklist(base):
    __tablename__ = 'checklists'
    id = Column(Integer, primary_key=True)
    participants = relationship('User', secondary='checklist_participants', back_populates='joined_checklists')
class User(base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    joined_checklists = relationship('Checklist', secondary='checklist_participants', back_populates='participants')
In order to give an extra column referring_user to the secondary table checklist_participants, I moved it into its own class like so:
# new class
class Participant(base):
    checklist_id = Column(Integer, ForeignKey('checklists.id', ondelete='cascade'), primary_key=True)
    checklist = relationship('Checklist', foreign_keys=checklist_id)
    user_id = Column(Integer, ForeignKey('users.id', ondelete='cascade'), primary_key=True)
    user = relationship('User', foreign_keys=user_id)
    # new column
    referring_user_id = Column(Integer, ForeignKey('users.id', ondelete='set null'))
    referring_user = relationship('User', foreign_keys=referring_user_id)
class User(base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    joined_checklists = relationship('Checklist', secondary='checklist_participants', back_populates='participants')
    # new relationship definition
    referred_participants = relationship('Participant', back_populates='referring_user')
Simply moving the join table into its own class didn't cause any problems. But when I introduced the new column and tried to run a simple query:
session.query(User).filter(User.id == input_id).scalar()
I got this error:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Checklist.participants - there are multiple foreign key paths linking the tables via secondary table 'checklist_participants'.  Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables.
What do I need to define on my models to fix this issue?


UserandParticipantafter adopting the association object pattern. Long story short, you need to explicitly tell the relationships betweenUserandParticipantwhich foreign key columns to use, as shown here: stackoverflow.com/questions/22355890/…. In the query you've shown you have no joins, so the error seems like it's caused by the relationships.foreign_keysargument. I edited the main post to show my changes and the new error. I don't understand why the error is related toChecklist.participants... MyParticipanttable just has a single foreign key toChecklist, so there should be no confusion over the key to choose.Checklist.participantsis still a relationship toUser, then there are multiple paths from checklist to user after the addition of the 2nd foreign key.Checklist.participantsrelationship which foreign_key on the secondary table has to be used to connect toUser? If I try to addforeign_keys='Participant.user_id'to thatChecklist.participantsrelationship, I get another new exception: `sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Checklist.participants - there are no foreign keys linking these tables via secondary table 'checklist_participants'.