Skip to content Skip to sidebar Skip to footer

SQLAlchemy: Multiple ForeignKeyConstraint Referencing The Same Destination Column

I'm trying to properly define the relations between a few tables that all use composite keys. The issue I am facing is that one Column used in those keys is common to all the table

Solution 1:

The solution is to specify the primaryjoin attribute of the PlayerMatch.player relationship in order for it to use both Player.id and Player.region, but to prevent from persisting the region coming from Player, as follows:

player = relationship(
        'Player',
        back_populates='matches',
        primaryjoin="and_(Player.id == foreign(PlayerMatch.player_id), "
                    "Player.region == PlayerMatch.region)"
    )

Post a Comment for "SQLAlchemy: Multiple ForeignKeyConstraint Referencing The Same Destination Column"