Skip to content

feat: fix polymorphic relationship descriptors and add test coverage#70

Open
tmgbedu wants to merge 7 commits into
mainfrom
feat/orm-relationship-fixes-and-tests
Open

feat: fix polymorphic relationship descriptors and add test coverage#70
tmgbedu wants to merge 7 commits into
mainfrom
feat/orm-relationship-fixes-and-tests

Conversation

@tmgbedu

@tmgbedu tmgbedu commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix __get__ descriptor in MorphMany, MorphOne, and MorphToMany to return self on class-level access (instance is None), preventing AttributeError on class-attribute access
  • Fix morph_map() in MorphOne and MorphToMany to use registry.Registry.get_morph_map() instead of the broken load_config().DB._morph_map
  • Fix get_record_key_lookup to use Registry._reverse_map for correct alias resolution (previously iterated the forward map in insertion order, finding auto-registered class names before explicit aliases)
  • Fix apply_query in MorphMany/MorphOne to pass the model instance to get_record_key_lookup, not the builder's model class
  • Add delete_attribute() to Attribute for cleaning up transient pivot columns after eager loading
  • Add table() and without_global_scopes() helpers to QueryBuilder
  • Add full test suites for BelongsToMany, MorphMany, MorphOne, and MorphToMany

Test plan

  • uv run pytest tests/masoniteorm/sqlite/relationships/ -v — all relationship tests pass
  • uv run pytest --ignore=tests/masoniteorm/postgres -v — full suite passes without regressions

🤖 Generated with Claude Code

tmgbedu and others added 2 commits May 28, 2026 13:18
…est coverage

- Add `if instance is None: return self` guard to MorphMany, MorphOne, MorphToMany __get__
- Fix morph_map() to use registry.Registry.get_morph_map() instead of broken load_config().DB._morph_map
- Fix get_record_key_lookup to use Registry._reverse_map for O(1) lookup and correct alias resolution
- Fix apply_query in MorphMany/MorphOne to pass the model instance (not the builder's class) to get_record_key_lookup
- Add delete_attribute() helper to Attribute for cleaning up transient pivot attributes
- Add table() and without_global_scopes() helpers to QueryBuilder
- Add tests for BelongsToMany, MorphMany, MorphOne, and MorphToMany relationships

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmgbedu
tmgbedu force-pushed the feat/orm-relationship-fixes-and-tests branch from 27dc66c to f8904f9 Compare May 28, 2026 22:28
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmgbedu
tmgbedu force-pushed the feat/orm-relationship-fixes-and-tests branch from f8904f9 to b62a524 Compare May 28, 2026 22:29
Cover the abstract BaseRelationship contract independent of the concrete
relationship query wiring: non-string name rejection, key/attribute
defaults, __set_name__ owner recording, __get__ descriptor caching, get_builder
registry resolution, __getattr__ builder delegation, joins() clause building,
and NotImplementedError for all 11 abstract hooks.
tmgbedu added 3 commits July 23, 2026 15:00
…kfill-1234

# Conflicts:
#	fastapi_startkit/src/fastapi_startkit/masoniteorm/models/model.py
The polymorphic relationships targeted a stale Model API that later drift
removed: get_primary_key_value(), get_primary_key(), and a non-existent
.builder property (all silently swallowed to None by Model.__getattr__),
and MorphOne/MorphToMany resolved morph_map() via an undefined load_config().

Rewire them against the current API:
- morph_map() -> Registry.get_morph_map(); drop load_config
- primary key via get_attribute(__primary_key__) / __primary_key__
- resolve the related builder through the registry/factory instead of .builder
- get_record_key_lookup() returns the explicit morph alias and raises for
  models that only carry an auto-registered class-name entry
- get_related() is async and no longer awaits a non-awaitable on no match

Closes the source bugs tracked in #741 and completes the MorphOne/MorphToMany
registry migration tracked in #883. All 25 sqlite morph tests pass.
@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

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.

1 participant