Replies: 3 comments
-
Have you had any luck solving this? This is a really basic database type relationship (example on sqlalchemy website of a billing address/shipping address is very common... |
Beta Was this translation helpful? Give feedback.
-
I'm running into a similar problem with a link table, so far I've tried 3 approaches based on the docs/issues/discussions. I've attempted it with plain sqlmodel, sqlachemy enriched sqlmodel, and linked tables from sqlmodel but no luck with any of them. Option 1from uuid import UUID, uuid4
from sqlmodel import SQLModel, Field, Relationship
class ParentChildLinkTable(SQLModel, table=True):
__tablename__ = 'parent_child_link'
parent_id: UUID = Field(foreign_key="nodes.id", primary_key=True)
child_id: UUID = Field(foreign_key="nodes.id", primary_key=True)
parent: 'Node' = Relationship(back_populates="children")
child: 'Node' = Relationship(back_populates="parents")
class Node(SQLModel, table=True):
__tablename__ = 'nodes'
id: UUID = Field(primary_key=True)
name: str
parents: list['Node'] = Relationship(back_populates="child")
children: list['Node'] = Relationship( back_populates="parent")
node_1 = Node(id=uuid4(), name='Node-1') Results in
Option 2from uuid import UUID, uuid4
from sqlmodel import SQLModel, Field, Relationship
class ParentChildLinkTable(SQLModel, table=True):
__tablename__ = 'parent_child_link'
parent_id: UUID = Field(foreign_key="nodes.id", primary_key=True)
child_id: UUID = Field(foreign_key="nodes.id", primary_key=True)
parent: 'Node' = Relationship(back_populates="children", sa_relationship_kwargs={"foreign_keys": "ParentChildLinkTable.parent_id"})
child: 'Node' = Relationship(back_populates="parents", sa_relationship_kwargs={"foreign_keys": "ParentChildLinkTable.child_id"})
class Node(SQLModel, table=True):
__tablename__ = 'nodes'
id: UUID = Field(primary_key=True)
name: str
parents: list['Node'] = Relationship(back_populates="child", sa_relationship_kwargs={"foreign_keys": "ParentChildLinkTable.parent_id"})
children: list['Node'] = Relationship( back_populates="parent", sa_relationship_kwargs={"foreign_keys": "ParentChildLinkTable.child_id"})
node_1 = Node(id=uuid4(), name='Node-1') Results in
I have the same issue if IO specify Option 3from uuid import UUID, uuid4
from sqlmodel import SQLModel, Field, Relationship
class ParentChildLinkTable(SQLModel, table=True):
__tablename__ = 'parent_child_link'
parent_id: UUID = Field(foreign_key="nodes.id", primary_key=True)
child_id: UUID = Field(foreign_key="nodes.id", primary_key=True)
parent: 'Node' = Relationship(back_populates="children")
child: 'Node' = Relationship(back_populates="parents")
class Node(SQLModel, table=True):
__tablename__ = 'nodes'
id: UUID = Field(primary_key=True)
name: str
parents: list['Node'] = Relationship(back_populates="child", link_model=ParentChildLinkTable)
children: list['Node'] = Relationship( back_populates="parent", link_model=ParentChildLinkTable)
node_1 = Node(id=uuid4(), name='Node-1') Results in
This stack-overflow answer provides an example using pure sqlalchemy but I'm unclear how this can be adapted to work with sqlmodel. Any help would be much appreciated, please let me know if this is better posted in a separate discussion. |
Beta Was this translation helpful? Give feedback.
-
I THINK this works: from sqlalchemy import Column, UUID as SqlAlchemyUUID, ForeignKey
from sqlmodel import Field, Relationship
_thing1 = Column(SqlAlchemyUUID, ForeignKey('things.id'))
_thing2 = Column(SqlAlchemyUUID, ForeignKey('things.id'))
class Widget(SQLModel, table=True):
__tablename__ = 'widgets'
thing_1_id: UUID = Field(sa_column=_thing1)
thing_2_id: UUID = Field(sa_column=_thing2)
thing_1: Thing = Relationship(sa_relationship=relationship('Thing', foreign_keys=_thing1))
thing_2: Thing = Relationship(sa_relationship=relationship('Thing', foreign_keys=_thing2)) |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
I am encountering an issue when configuring a relationship between tables using SQLModel. I receive the following error message:
"Could not determine join condition between parent/child tables on relationship Subfolder.users - there are multiple foreign key paths linking the tables via secondary table 'user_subfolder_permission'. 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."
How can I specify the foreign key correctly when there are two foreign keys to the same table? Below are the models I am using:
Beta Was this translation helpful? Give feedback.
All reactions