fix: create_schema crashes on models with a ForeignObject reverse relation - #1746
Open
shivanij1203 wants to merge 1 commit into
Open
fix: create_schema crashes on models with a ForeignObject reverse relation#1746shivanij1203 wants to merge 1 commit into
shivanij1203 wants to merge 1 commit into
Conversation
…ation The field filter only skipped ManyToOneRel and ManyToManyRel, so the bare ForeignObjectRel that a ForeignObject field creates on the target model fell through and hit .help_text, which it does not have. Skip the ForeignObjectRel base class instead, which covers every reverse relation. Closes vitalik#1530.
mbaragiola
reviewed
Jul 19, 2026
mbaragiola
left a comment
Contributor
There was a problem hiding this comment.
Positives
- Generalizing the reverse-relation filter to
ForeignObjectRelcovers the bare reverse object while retaining the existingManyToOneRelandManyToManyRelbehavior through inheritance. - The regression test reproduces the crashing relation shape, verifies schema creation, and checks that the reverse accessor is excluded while normal fields remain.
Residual risk
- The new test covers the default depth path only, although the shared field-selection path makes a depth-specific regression unlikely.
Verification
I reviewed the relation hierarchy, schema field-selection path, and regression test against master. GitHub currently reports no checks for this head; I did not run the test suite locally.
Reviewed by Hermes Agent using Codex gpt-5.6-sol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Generating a
ModelSchema/ callingcreate_schema()for a model that is the target of aForeignObjectrelation crashes with:SchemaFactory._model_fieldsskips reverse relations, but it only checked forManyToOneRelandManyToManyRel. AForeignObjectfield creates a bareForeignObjectRel(the base class of those two) on the target model, so it fell through the filter and reachedfield.help_textinget_schema_field, which a reverse relation does not have.Since
ManyToOneRelandManyToManyRelboth subclassForeignObjectRel, filtering on the base class covers every reverse relation, including the bareForeignObjectRel.Fixes #1530
Changes
ninja/orm/factory.py: skipForeignObjectRel(the reverse-relation base class) instead of only its two subclasses. This also let the now-redundantcast(DjangoField, fld)be removed, sincefldis narrowed toFieldafter the check.tests/test_orm_relations.py: regression test usingisolate_appsso the reverseForeignObjectRelactually registers.Testing
pytest tests/test_orm_relations.py tests/test_orm_metaclass.py tests/test_models.py tests/test_django_models.pypasses.mainwith the exactAttributeError: 'ForeignObjectRel' object has no attribute 'help_text', and passes with the fix.ruff format --check,ruff check, andmypy ninja/orm/factory.pyare clean.Minimal reproduction (from the issue): a model
OrderDetailwithmodels.ForeignObject(Order, ..., related_name="details"), thencreate_schema(Order).