0

I am trying to use SQLModel to create a graph Node that would keep it's connections/links in it so that it can be traversed and stored in the database.

I started of upgrading the Node class defined in other SQL answer, which works fine but is designed for One-to-Many link.

I have added "links" field:

class Node(SQLModel, table=True):
    ...
    id: Optional[int] = Field(default=None, primary_key=True)
    ...
    links: list['Node'] = Relationship(back_populates='links', link_model=NodeLink)

...and a "NodeLink" model for Many-to-Many to work:

class NodeLink(SQLModel, table=True):
    this_id: Optional[int] = Field(
        default=None, foreign_key="node.id", primary_key=True
    )
    other_id: Optional[int] = Field(
        default=None, foreign_key="node.id", primary_key=True
    )

...but this does not work. Can someone nudge me to correct me to the right track?

NB: Finally, my plan is to add weights to the nodes and making links updated automatically for both connected nodes when one gets linked.

0 Answers0