First Check
Commit to Help
Example Code
from typing import List, Optional
from sqlmodel import Field, Relationship, SQLModel
class Edge(SQLModel, table=True):
from_node_id: Optional[int] = Field(default=None, foreign_key="node.id", primary_key=True)
to_node_id: Optional[int] = Field(default=None, foreign_key="node.id", primary_key=True)
class Node(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
from_nodes: List["Node"] = Relationship(back_populates="to_nodes", link_model=Edge)
to_nodes: List["Node"] = Relationship(back_populates="from_nodes", link_model=Edge)
Description
I want to be able to create a many to many relationship between a table and itself. In my example, I have a table Node and a table Edge which I'm using to represent a graph. I want the table Edge to work as a link table establishing a many to many relationship between the table Node and itself. Like that, I would be able to benefit from the SQLModel's lazy loading when querying the nodes and also get the in and out adjacency lists for each node very efficiently.
Please, let me know if it's already possible somehow.
Wanted Solution
The solution I am thinking about is to add a field parameter to the Relationship callable. Check out my wanted code.
Wanted Code
from typing import List, Optional
from sqlmodel import Field, Relationship, SQLModel
class Edge(SQLModel, table=True):
from_node_id: Optional[int] = Field(default=None, foreign_key="node.id", primary_key=True)
to_node_id: Optional[int] = Field(default=None, foreign_key="node.id", primary_key=True)
class Node(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
from_nodes: List["Node"] = Relationship(back_populates="to_nodes", link_model=Edge, field="to_node_id")
to_nodes: List["Node"] = Relationship(back_populates="from_nodes", link_model=Edge, field="from_node_id")
Alternatives
I couldn't think about any decent alternative. Please, let me know if you have another idea.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
Python 3.9.12
Additional Context
No response
First Check
Commit to Help
Example Code
Description
I want to be able to create a many to many relationship between a table and itself. In my example, I have a table
Nodeand a tableEdgewhich I'm using to represent a graph. I want the tableEdgeto work as a link table establishing a many to many relationship between the tableNodeand itself. Like that, I would be able to benefit from the SQLModel's lazy loading when querying the nodes and also get the in and out adjacency lists for each node very efficiently.Please, let me know if it's already possible somehow.
Wanted Solution
The solution I am thinking about is to add a
fieldparameter to theRelationshipcallable. Check out my wanted code.Wanted Code
Alternatives
I couldn't think about any decent alternative. Please, let me know if you have another idea.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
Python 3.9.12
Additional Context
No response