fix(database): resolve uuid primary keys from positional insert bindings - #2301
radoslav-grencik wants to merge 4 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 dbe65d6 |
What this PR doesFixes relation auto-inserts for models with 1. Bug fix:
|
|
What happens when an insert explicitly passes a |
Good catch — that was a bug. When an insert explicitly passes MySQL and PostgreSQL both treat Fix: treat $value = $query->bindings[$index] ?? null;
// 0 is treated as "auto-increment" by MySQL/PostgreSQL — not a real id.
if ($value === 0 || $value === null) {
return null;
}
return $value;This is safe for UUID models too — a UUID entry never contains Added a test ( |
| /** | ||
| * Adds a UUID column to the table. Uses `CHAR(36)` for MySQL, `UUID` for PostgreSQL, and `TEXT` for SQLite. | ||
| */ | ||
| public function uuidColumn(string $name, bool $nullable = false): self |
There was a problem hiding this comment.
This function name is inconsistent with our other names. We should probably repurpose uuid for this instead, and use primary as the sole way of creating primary UUIDs.
Would be breaking though, so for next major.
Fixes #2300