Replies: 2 comments
-
Try this: voted_player: "Player" = Relationship(sa_relationship_kwargs={"foreign_keys": [voted_player_id]}) to voted_player: "Player" = Relationship(sa_relationship_kwargs={"foreign_keys": "voted_player_id"}) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I had a similar issue and ended up here, finding no answer. I fixed this by using the sa_relationship_kwargs slightly differently, here is a distilled example of what I had: class ChatSession(SQLModel, table=True):
id: int | None = Field(default=None, index=True, primary_key=True)
user_id: int = Field(foreign_key="user.id")
other_user_id: int = Field(foreign_key="user.id")
user: "User" = Relationship()
other_user: "User" = Relationship() This gave a similar error (though I realize not the same) as op. sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper[ChatSessionExample(chatsessionexample)]'. Original exception was: Could not determine join condition between parent/child tables on relationship ChatSessionExample.user - 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. The following is the result that ended up fixing this error. class ChatSessionExample(BaseIndexedDbModel, table=True):
user_id: int = Field(foreign_key="user.id")
other_user_id: int = Field(foreign_key="user.id")
user: "User" = Relationship(
sa_relationship_kwargs={"foreign_keys": "ChatSessionExample.user_id"}
)
other_user: "User" = Relationship(
sa_relationship_kwargs={"foreign_keys": "ChatSessionExample.other_user_id"}
) I'm pretty new to SQLModel, so if anyone knows this is not an ideal workaround or a better one exists, please say so! I'll try and update this comment with any necessary changes. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
When trying to have two references to a model as I have in the case of the Vote class to the player class where I want to know who voted but also to who that user voted I'm running into some relationship problems that are displaying that we don't offer the same
foreign_keys
field offered by SQLAlchemy, which would be the workaround for this?Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.16
Python Version
3.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions