I am trying to create a model with multiple many-to-many relationships referencing self. See example code.
My code raises this error:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship User.parents - there are multiple foreign key paths linking the tables via secondary table 'userlink'. 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
My code is based on the many-to-many example in the docs.
As the error is coming from sqlalchemy it might also be an sqlalchemy issue but as it is raised by an example that is coming from the docs I think I might be using SQLmodel incorrectly.
I did read the sqlalchemy docs on many-to-many relationships but they did not help me.
This issue on stackoverflow seems related but I cannot figure out how to apply it to my situation.
Questions:
Am I correct in the assumption that I need to use many-to-many relationships with a link model?
...or could the same be achieved by using a one-to-many with back_populates?
The sqlalchemy error is quite cryptic (at least to me). I seem to already have specified the foreign_key on the UserLink model columns but apparently I need to specify it differently or somewhere else as well. What else is needed to make this work?
Reacted by Aart Goossens, Ishan Ghosh, Evan Thomas, Toni Alatalo, astroboy, Beto Dealmeida, draew6, liquidgenius, Filipp Frizzy, Jie (Edward) Xu and 8 more
I think I tried something similar by passing sqlalchemy.orm.relationship() directly to sqlmodel.Relationship() as the sa_relationship kwarg but I must have missed something there. Using sa_relationship_kwargs also seems a bit nicer as you do not have to import from sqlalchemy.
And thanks for the link to the documentation, I did not find this page before. It really helps to understand what information/configuration SQLmodel/sqlalchemy need to make these self-referential relationships.
None of this stuff is documented on the SQLModel side, and I wouldn't be surprised if tiangolo wants to solve this in some more elegant way. Maybe it's better to leave this issue open until a maintainer has a look?
Reacted by Evan Thomas, Ayrton Bourn, Rohan Karan, rysson and 朱凌峰
Activity
maresb commented on Sep 12, 2021
I'm fairly new to this, but I seem to have managed to get it to work with
I'm not sure whether or not this is the "correct" way. I'm curious about what more expert people have to say.
My inspiration was Self-Referential Many-to-Many Relationship from the SQLAlchemy docs.
AartGoossens commented on Sep 13, 2021
@maresb This works! You are my hero! 🦸 Awesome.
I think I tried something similar by passing sqlalchemy.orm.relationship() directly to sqlmodel.Relationship() as the sa_relationship kwarg but I must have missed something there. Using sa_relationship_kwargs also seems a bit nicer as you do not have to import from sqlalchemy.
And thanks for the link to the documentation, I did not find this page before. It really helps to understand what information/configuration SQLmodel/sqlalchemy need to make these self-referential relationships.
Thanks a lot!
maresb commented on Sep 13, 2021
I'm glad I could help!
None of this stuff is documented on the SQLModel side, and I wouldn't be surprised if tiangolo wants to solve this in some more elegant way. Maybe it's better to leave this issue open until a maintainer has a look?
AartGoossens commented on Sep 13, 2021
That's a good point. I'll reopen the issue.
cashewpillar commented on May 18, 2022
I tried the
But I get the error
Where did I make a mistake?
AartGoossens commented on May 18, 2022
@Iionsroar I think this is not related to this issue, but this one #322.