2 relationships of the same tablename confusing SQLAlchemy
-
28-10-2019 - |
Question
IE:
class Share(db.Model):
"""
Stores the Checkpoint shares between users
"""
id = db.Column(db.Integer, primary_key=True)
user_from_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user_from = db.relationship("User")
user_to_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user_to = db.relationship("User")
I get an error:
ArgumentError: Could not determine join condition between parent/child tables on relationship Share.user_from. Specify a 'primaryjoin' expression. If 'secondary' is present, 'secondaryjoin' is needed as well.
How can I fix this?
Thanks!
Solution
As the error message suggests, you need to specify primaryjoin
value for your relationships:
class Share(db.Model):
# ...
user_from = relationship("User", primaryjoin="Share.user_from_id==User.id")
user_to = relationship("User", primaryjoin="Share.user_to_id==User.id")
# ...
See Specifying Alternate Join Conditions to relationship() for more information.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow