fix(database): auto resolve queries for new relations on insert and update#2058
Merged
Conversation
…anyThrough, and HasOneThrough relations InsertQueryBuilder.resolveObjectData() crashes with ReflectionException when a model has BelongsToMany, HasManyThrough, or HasOneThrough properties. These relations use pivot/intermediate tables and should be skipped during insert, but the method falls through to resolveRelationProperty() which calls asClass() on type 'array'. Adds Tag, BookReview, and Reviewer fixture models and three failing test cases that reproduce the bug.
…in InsertQueryBuilder These relations use pivot/intermediate tables and have no column on the model itself. Without this, resolveObjectData() falls through to resolveRelationProperty() which calls asClass() on type 'array', throwing ReflectionException.
…ilder When inserting a model with a BelongsToMany relation, related items with existing PKs get pivot rows created automatically. New related items without PKs are inserted first, then linked via pivot. Follows the same after-callback pattern as HasMany/HasOne. Adds CreateTagTable and CreateBookTagTable migrations for testing.
…h in UpdateQueryBuilder UpdateQueryBuilder had the same bug as InsertQueryBuilder — BelongsToMany/through relations fell through to resolveRelationValue() which calls asClass() on array type. BelongsToMany updates now delete existing pivot rows and re-insert (sync behavior). HasManyThrough and HasOneThrough are skipped as they require intermediate models.
…th new relation types Ensures BelongsToMany, HasManyThrough, and HasOneThrough relations on models don't break Select (including with() eager loading), Count, or Delete query builders. Also applies Mago formatting.
Benchmark ResultsComparison of Open to see the benchmark resultsNo benchmark changes above ±5%. Generated by phpbench against commit 4e77736 |
…UpdateQueryBuilder
…rt related models No need to pre-create books — the callback handles inserting new related items and creating pivot rows in one step.
brendt
approved these changes
Mar 21, 2026
Member
|
Thanks! |
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.
@brendt shipped a bug yesterday, didn't. notice queries handle inserting/updating relations separately