Repository navigation
Fix MorphMany/MorphOne loads when the morph name equals the class name - #338
Merged
Merged
Conversation
get_record_key_lookup treated a morph name equal to the class name as an unregistered model and raised ValueError, but every Model registers its class name as the default morph name, so unaliased models could never be eager or lazy loaded. Return the registry's morph name as-is, and forward nested eagers in MorphMany/MorphOne.get_related. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Problem
Lexeme.with_("media")(aMorphManywith morph name"Lexeme") raisedValueError: Could not find the record type key for the <Lexeme> class.load("media")andMorphOnefailed the same way.Root cause
MorphMany/MorphOne.get_record_key_lookuptreatedRegistry.get_morph_name(cls) == cls.__name__as "model not registered". ButModel.__init_subclass__registers every model with its class name as the default morph name. So every model without amorph_mapalias, or mapped to its own class name, always hit the error. The guard could never catch a real missing model.Changes
get_record_key_lookupreturnsRegistry.get_morph_name(cls)as-is.morph_mapaliases still take priority.MorphMany/MorphOne.get_relatednow forward nested eagers (.with_(eagers or [])), the same wayHasMany/HasOnedo, sowith_("media.lexeme")works.test_morph_onecase that asserted the old error. It now asserts the class-name default.MorphTo/MorphToManywere not affected and are unchanged.Tests
New
tests/masoniteorm/sqlite/relationships/test_morph_name_equals_class_name.py:MorphManyeager load on a collection and onfirst(),MorphMany/MorphOnelazyload,MorphOneeager load,MorphToeager load, and nestedmedia.lexeme/first_medium.lexeme. Without the fix, 7 of the 8 original cases fail.uv run pytest --ignore=tests/masoniteorm/postgres --cov: 3727 passed, coverage 92.75%.🤖 Generated with Claude Code