Skip to content

📝 Add docs page for self-referential model#408

Open
daniil-berg wants to merge 18 commits into
fastapi:mainfrom
daniil-berg:main
Open

📝 Add docs page for self-referential model#408
daniil-berg wants to merge 18 commits into
fastapi:mainfrom
daniil-berg:main

Conversation

@daniil-berg
Copy link
Copy Markdown
Contributor

@daniil-berg daniil-berg commented Aug 26, 2022

Purpose

Adds a new page explaining how to construct a self-referential/recursive relationship with SQLModel to the Advanced User Guide section of the documentation. This is arguably a fairly common use case that has been asked about and discussed in issue #127 already.

Code

The example code uses a Villain class/model for demonstration in an attempt at honoring the Hero theme of the documentation. A villain can have a boss villain and villain minions.

Additional info

The new page also includes a brief explanation of the sa_relationship_kwargs parameter of the Relationship constructor, since that is essential for defining the desired relationship. Depending on how @tiangolo wants to structure the rest of the advanced guide, this section may either need to be expanded or instead abbreviated to a short mention and link to a dedicated section for sa_-style parameters in SQLModel fields and relationships in the future.

Update (2022-08-02)

Rebased onto most recent version of the main branch (0.0.8 release, 75ce455).

Then rebased again, after adding tests to cover the new documentation example code. Coverage of the added code should now be at 100% (see latest codecov checks).

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@codecov

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@tiangolo tiangolo added the docs Improvements or additions to documentation label Oct 22, 2023
@jmehnle
Copy link
Copy Markdown

jmehnle commented Jan 7, 2025

@tiangolo, what's blocking this from being merged?

@iloveitaly
Copy link
Copy Markdown

This is some really helpful documentation. @svlandeg anything we can do to get this merged?

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

Copy link
Copy Markdown
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniil-berg, thanks for working on this!

Great job!
I suggested a few minor improvements. Could you please take a look?

Also, we should probably create a group in Advanced (e.g. "Advanced Relationships") for other relationships-related sections such as #815

Comment thread docs/advanced/self-referential.md Outdated
Comment thread docs/advanced/self-referential.md Outdated
Comment thread docs/advanced/self-referential.md Outdated
Comment thread docs/advanced/self-referential.md Outdated
@github-actions

This comment was marked as outdated.

Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
@svlandeg svlandeg self-assigned this Sep 17, 2025
@github-actions

This comment was marked as outdated.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Oct 8, 2025

📝 Docs preview

Last commit 53809d1 at: https://fef55e49.sqlmodel.pages.dev

Modified Pages

Copy link
Copy Markdown
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't review in detail (as Yurii already did 🙌), but just a few small nitpicks 😇

Comment thread docs/advanced/advanced-relationships/self-referential.md Outdated
Comment thread docs/advanced/advanced-relationships/self-referential.md Outdated
@svlandeg svlandeg removed their assignment Oct 21, 2025
@RanaUniverse
Copy link
Copy Markdown

So currently what is the link of this self-reference docs link?
i wanted to make one for some days i was in trouble to found a way, as i dont know properly how to use sqlalchemy, so i wanted if there was any way in sqlmodel?

@RanaUniverse
Copy link
Copy Markdown

📝 Docs preview

Last commit 53809d1 at: https://fef55e49.sqlmodel.pages.dev

Modified Pages

Today i read this docs, thinking the Villain example i made like this, i found and understand your explanations, i give you thanks for this easy explanation.

See i wanted to make a billing system where i need to keep record of product and its category, and i found category can have its own category like this, so after the Villain example you give in your article here i found i did something like this,

image I am not properly sure is this a right way to use like this? or any problem it can happens here? Can you please check the `parent_id` & `parent_obj` field in the Sqlmodel class i made for category

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants