I want a user to be referral and referee, and store some additional data. Here is my code:
class User(UserMixin, db.Model):
__tablename__ = 'user'
referees = db.relationship('RefData', back_populates='referral', lazy='dynamic')
referrals = db.relationship('RefData', back_populates='referee', lazy='dynamic')
and the RefData table:
class RefData(UserMixin, db.Model):
__tablename__ = 'ref_data'
referral_id = db.Column(UUID(as_uuid=True), db.ForeignKey('user.id'), primary_key=True)
referral = db.relationship('User', back_populates='referrals')
referee_id = db.Column(UUID(as_uuid=True), db.ForeignKey('user.id'), primary_key=True)
referee = db.relationship('User', back_populates='referees')
But with this code, I have the following exception and I can’t figure out how to solve it:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between
parent/child tables on relationship User.referees - there are multiple foreign key paths
linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns
which should be counted as containing a foreign key reference to the parent table.