Repository navigation
fix(database): attach batch inserted relations to correct parent - #2320
Open
radoslav-grencik wants to merge 5 commits into
Open
radoslav-grencik wants to merge 5 commits into
radoslav-grencik wants to merge 5 commits into
Conversation
…n keys Iterable inserts (`insert()` with array data) never generated `#[Uuid]` primary keys — only the object path (`create()`, `save()`) did — so UUID models inserted this way ended up with a NULL key (error 1364 on MySQL). Migrations also had no way to define UUID-typed foreign key columns: `belongsTo()`/`foreignId()` always compile INTEGER, which cannot reference a UUID primary key (fails on PostgreSQL). - generate `#[Uuid]` values in the iterable insert path when the key is absent (explicitly provided ids are never overwritten) - add `uuidColumn()`, `belongsToUuid()` and `foreignUuid()` to `CreateTableStatement` (CHAR(36) / UUID / TEXT per dialect)
Benchmark ResultsComparison of Open to see the benchmark results
Generated by phpbench against commit ba61e50 |
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
Batch inserts with relations silently attached every row's children to the
first parent:
insert($a, $b)ran all relation callbacks with$a'sprimary key, so
$b's HasMany / HasOne / BelongsToMany children were writtenwith the wrong foreign key.
Before: both posts got Frieren's id.
After: each post gets its own parent's id.
Implementation
Relation callbacks are now grouped per row during data resolution
(
relationCallbacks, sliced per resolved row alongsideresolvedRows).At execution time each row's callbacks receive that row's id:
runs, so it is read back from the resolved row data
(
resolveRowPrimaryKey()) and passed to the row's own callbacks.No dialect-specific behavior involved.
behavior. Per-row ids are not reliably recoverable from a multi-row
INSERT across dialects — MySQL returns the first auto-increment id,
SQLite the last one, and PostgreSQL
RETURNINGhas no guaranteed roworder to map back to input rows. Supporting that correctly would require
falling back to per-row inserts inside a transaction; out of scope until
a concrete use case appears.
then()callbacks are unaffected (still receive the execute-time id).