How to implement a junction/association table with multiple relationships #792
Unanswered
franTarkenton
asked this question in
Questions
Replies: 2 comments
-
|
@franTarkenton Were you ever able to figure it out? I have the same usecase. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I think you need to add So, it will look like |
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
In summary this is what I'm trying to implement:
erDiagram HEROS { int id PK string name string secret_name } TEAM { int id PK string name string headquarters } TEAM_POSITION { int id PK string position_name } HERO_TEAM_LINK { int team_id FK int hero_id FK int position_id FK } HEROS || --|{ HERO_TEAM_LINK: "relationship to position/team" HERO_TEAM_LINK || --|| TEAM_POSITION: "has" HERO_TEAM_LINK || --|| TEAM: "has"I copied the example from many to many page and modified it to add the position table. What I want is the junction table to manage the relationship of pairs of team/position.
Unfortunately when I try to do something like:
.... hero_borat = Hero( name="Sasha Baron Cohen", secret_name="Borat Sagdiyev", teams=[team_preventers], position=[team_positions_manager], ) session.add(hero_borat) session.commit()I get integrity constraints because its trying add to one of the related tables at a time. You can see in the example code how I am working around this by:
team_preventers.idandteam_position_member.idhero_team_linkIs this the best way?
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.14
Python Version
3.11.7
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions