Skip to content

fix: stop recursing on a required self-referencing foreign key - #2066

Open
itzzdev09 wants to merge 1 commit into
ormar-orm:masterfrom
itzzdev09:fix/self-referencing-required-fk
Open

itzzdev09 wants to merge 1 commit into
ormar-orm:masterfrom
itzzdev09:fix/self-referencing-required-fk

Conversation

@itzzdev09

Copy link
Copy Markdown
Contributor

Fixes #2061

key_only_instance fills every required relation of the target with a -1 placeholder built by calling itself on the related model. For a required self-reference (or any cycle of required relations) the placeholder needs a placeholder of its own, so Node(parent=1) and loading such a row hit RecursionError.

The fix tracks the models already being built in the current chain and leaves a relation unset when it points back at one of them. The nested placeholder then behaves like the nullable case (node.parent.parent is None), while non-cyclic required relations still get their placeholders as before.

Tests (tests/test_relations/test_required_self_reference.py):

  • Node(parent=1) for a required self-reference
  • a two-model cycle of required relations (Chicken.egg / Egg.chicken)
  • create + get + select_related("parent").get round-trip against the database

All three fail on master with RecursionError and pass with the change. Full suite on SQLite: the only failures are the 7 test_pool_reset MySQL cases that need asyncmy/aiomysql, same as on master.

Key-only instances fill required relations of the target with -1
placeholders. When the relation points back at a model whose placeholder
is already being built (a required self-reference, or a cycle of
required relations), the placeholder needed another placeholder of its
own without end and raised RecursionError on Node(parent=1) and when
loading such rows. Leave those relations unset instead.

Fixes ormar-orm#2061
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Dec 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 122 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 143 benchmarks have no flamegraph

The flamegraph wasn't generated: this Python version can't be profiled. Use CPython 3.12 or later on Linux x86-64 or ARM64 with glibc, e.g., from actions/setup-python or uv.

See affected benchmarks

✅ 143 untouched benchmarks


Comparing itzzdev09:fix/self-referencing-required-fk (15a3c22) with master (3fcfd93)

Open in CodSpeed

This branch has not been deployed

No deployments
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.

Non-nullable self-referencing ForeignKey raises RecursionError when building an instance

2 participants