Skip to content

Support self referential tables for SQLModels#467

Merged
sheinbergon merged 5 commits intoagronholm:masterfrom
sheinbergon:sqlmodel-self-referential-table
Feb 27, 2026
Merged

Support self referential tables for SQLModels#467
sheinbergon merged 5 commits intoagronholm:masterfrom
sheinbergon:sqlmodel-self-referential-table

Conversation

@sheinbergon
Copy link
Collaborator

@sheinbergon sheinbergon commented Feb 25, 2026

Changes

Add support for self-referential tables for SQLModels

For a tabe definition like

Table(
    "simple_items",
    generator.metadata,
    Column("id", INTEGER, primary_key=True),
    Column("parent_item_id", INTEGER, ForeignKey("simple_items.id")),
)

The generated output changes from:

sa_relationship_kwargs={'remote_side': [id]}

to:

sa_relationship_kwargs={'remote_side': '[SimpleItems.id]'}

Fixes #460

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) which would fail without your patch
  • You've added a new changelog entry (in CHANGES.rst).

@sheinbergon sheinbergon added this to the 4.0.2 milestone Feb 25, 2026
@sheinbergon
Copy link
Collaborator Author

@agronholm coveralls is currently down, but tests are passing locally. It's ready for review

@agronholm
Copy link
Owner

I'll review this tomorrow.

sheinbergon and others added 2 commits February 26, 2026 18:45
Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
@sheinbergon
Copy link
Collaborator Author

@StrikerRUS can you confirm the code on my branch addresses the errors you've encountered?

Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

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

Looks good now.

@StrikerRUS
Copy link

@sheinbergon Thanks a lot for the PR! Will check this weekend.

Copy link

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

@sheinbergon
Just checked your branch - my initial example now works fine without errors. Thank you so much for prompt fix!

@sheinbergon
Copy link
Collaborator Author

@agronholm coveralls is still down (what a blunder), but test are passing locally. I'll take the liberty of merging this PR.

@sheinbergon sheinbergon merged commit 72a20be into agronholm:master Feb 27, 2026
2 of 7 checks passed
@sheinbergon sheinbergon deleted the sqlmodel-self-referential-table branch February 27, 2026 19:04
@agronholm
Copy link
Owner

The issue with the Coveralls site isn't even technical – it's an account issue with their infrastructure provider.

@baraknaveh
Copy link

Thanks for the fix!

@sheinbergon
Copy link
Collaborator Author

Thanks for the fix!

Ein ba'ad ma

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlmodels generator cannot generate correct code for self referential table

4 participants