Skip to content

Currently I'm having a problem when trying to have two different foreign keys to the same Model #829

Answered by JoelYoung01
Miguelme asked this question in Questions
Discussion options

You must be logged in to vote

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[ChatSession…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants