First Check
Commit to Help
Example Code
class Node(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
text: str
parent_id: Optional[int] = Field(foreign_key="node.id")
# parent: Optional["Node"] not sure what to put here
# children: List[Node] not sure what to put here either :)
Description
I am trying to create a simple self referential model - using the SQL model equivalent of the adjacency list pattern described here: https://docs.sqlalchemy.org/en/14/orm/self_referential.html
I am only a litte familiar with SQL alchemy but was unable to translate their example into one that would work with SQLmodel.
In your docs you said: "Based on SQLAlchemy [...] SQLModel is designed to satisfy the most common use cases and to be as simple and convenient as possible for those cases, providing the best developer experience". I was assuming that a self referential model would be a fairly common use case but totally appreciate that I could be wrong on this :)
I see that there is an sa_relationship param that you can pass 'complicated stuff' too but I was not sure whether I should be using that (or how I would do so if I was meant to) - sorry just a bit too new to this.
Crossing my fingers that it is straight forward to complete the commented lines in my example.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.9.7
Additional Context
No response
First Check
Commit to Help
Example Code
Description
I am trying to create a simple self referential model - using the SQL model equivalent of the adjacency list pattern described here: https://docs.sqlalchemy.org/en/14/orm/self_referential.html
I am only a litte familiar with SQL alchemy but was unable to translate their example into one that would work with SQLmodel.
In your docs you said: "Based on SQLAlchemy [...] SQLModel is designed to satisfy the most common use cases and to be as simple and convenient as possible for those cases, providing the best developer experience". I was assuming that a self referential model would be a fairly common use case but totally appreciate that I could be wrong on this :)
I see that there is an
sa_relationshipparam that you can pass 'complicated stuff' too but I was not sure whether I should be using that (or how I would do so if I was meant to) - sorry just a bit too new to this.Crossing my fingers that it is straight forward to complete the commented lines in my example.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.9.7
Additional Context
No response