Many-many relationship challenges when the link table only uses one of the FK as a primary #586
-
First Check
Commit to Help
Example Codeclass Band_Member2(SQLModel,table=True):
__table_name__ = "band_member"
status: str = Field( default=None,description="Member Status code", sa_column_kwargs={"name":"member_status_code"})
band_id: int = Field(
default=None,
description="Band ID",
foreign_key="band.band_id",
)
member_id: int = Field(
default=None,
description="Member ID",
foreign_key="person.person_id",
primary_key=True,
)
join_date: Optional[datetime] = Field(
default=None, description="Date the band member joined the band"
)
class Person2(SQLModel, table=True):
__table_name__ = "person"
id: int = Field(default=None, primary_key=True)
first_name: str = Field(default=None,sa_column_kwargs={"name":"first_name"})
last_name: str = Field(default=None,sa_column_kwargs={"name":"last_name"})
band_links: List["Band_Member2"] = Relationship(back_populates="member", link_model=Band_Member2)
class Band2(SQLModel, table=True):
__table_name__ = "band"
id: int = Field(default=None, primary_key=True)
name: str = Field(default=None,sa_column_kwargs={"name":"band_name"})
band_members: List["Band_Member2"] = Relationship(back_populates="band", link_model=Band_Member2)
@app.get("/bands", response_model=List[Band])
def read_band(*, session: Session = Depends(get_session)):
return session.exec(select(Band)).all() Description
Get an error "Could not determine join condition between parent/child tables on relationship Person.band_links" I've narrowed the code down to the simplest possible definition (I've had to rename the tables and objects not to conflict with other parts of my project). I feel like this matches the Team/Hero example in the tutorial exactly, but I have spent a few hours re-reading the tutorial, searching on-line and trying every conceivable combination without any progress. I'm sure it's something simple, but I sure could use some help. Operating SystemLinux Operating System DetailsNo response SQLModel VersionSQLModel 0.0.8 Python VersionPython 3.11.2 Additional ContextI didn't include all the details to create the database and all, but essentially the database has the following tables Band Person Band_Member |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I feel like that primary keys in each table should be like this:
|
Beta Was this translation helpful? Give feedback.
I feel like that primary keys in each table should be like this: