diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index a1a04f82..0910e2fa 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -165,12 +165,7 @@ protected function buildDefinition(Model $model): string if ($column->name() === 'id' && $dataType === 'id') { $dataType = 'bigIncrements'; } elseif ($dataType === 'id') { - if ($model->isPivot()) { - // TODO: what if constraints are enabled? - $dataType = 'foreignId'; - } else { - $dataType = 'unsignedBigInteger'; - } + $dataType = 'foreignId'; } if (in_array($dataType, self::UNSIGNABLE_TYPES) && in_array('unsigned', $column->modifiers())) { diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php index 1569e711..dcf195fb 100644 --- a/src/Lexers/ModelLexer.php +++ b/src/Lexers/ModelLexer.php @@ -178,7 +178,7 @@ private function buildModel(string $name, array $columns): Model if ($type === 'belongsTo') { $column = $this->columnNameFromRelationship($relationship); if (isset($columns[$column]) && !str_contains($columns[$column], ' foreign') && !str_contains($columns[$column], ' id')) { - $columns[$column] .= ' id:' . Str::before($relationship, ':'); + $columns[$column] = trim($this->removeDataTypes($columns[$column]) . ' id:' . Str::before($relationship, ':')); } } } @@ -215,7 +215,7 @@ private function buildColumn(string $name, string $definition): Column $data_type = null; $modifiers = []; - $tokens = preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition); + $tokens = $this->parseColumn($definition); foreach ($tokens as $token) { $parts = explode(':', $token); $value = $parts[0]; @@ -330,4 +330,19 @@ private function hasBelongsToRelationship(Model $model, string $reference): bool return false; } + + private function removeDataTypes(string $definition): string + { + $tokens = array_filter( + $this->parseColumn($definition), + fn ($token) => strtolower($token) !== 'unsigned' && !isset(self::$dataTypes[strtolower($token)]) + ); + + return implode(' ', $tokens); + } + + private function parseColumn(string $definition): array + { + return preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition); + } } diff --git a/tests/Feature/Lexers/ModelLexerTest.php b/tests/Feature/Lexers/ModelLexerTest.php index 3d64cb84..8f6a67c6 100644 --- a/tests/Feature/Lexers/ModelLexerTest.php +++ b/tests/Feature/Lexers/ModelLexerTest.php @@ -681,7 +681,7 @@ public function it_infers_belongsTo_columns(): void $this->assertEquals([], $columns['id']->modifiers()); $this->assertEquals('venue_id', $columns['venue_id']->name()); $this->assertEquals('id', $columns['venue_id']->dataType()); - $this->assertEquals(['unsigned'], $columns['venue_id']->modifiers()); + $this->assertEquals([], $columns['venue_id']->modifiers()); $this->assertEquals(['Venue'], $columns['venue_id']->attributes()); $this->assertEquals('region_id', $columns['region_id']->name()); $this->assertEquals('id', $columns['region_id']->dataType()); diff --git a/tests/fixtures/migrations/belongs-to-many.php b/tests/fixtures/migrations/belongs-to-many.php index 91b9f287..a6ec5a92 100644 --- a/tests/fixtures/migrations/belongs-to-many.php +++ b/tests/fixtures/migrations/belongs-to-many.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('journeys', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->unsignedBigInteger('user_id'); + $table->foreignId('user_id'); $table->timestamps(); }); } diff --git a/tests/fixtures/migrations/identity-columns-big-increments.php b/tests/fixtures/migrations/identity-columns-big-increments.php index 0c0f6c33..dcf8f54e 100644 --- a/tests/fixtures/migrations/identity-columns-big-increments.php +++ b/tests/fixtures/migrations/identity-columns-big-increments.php @@ -13,8 +13,8 @@ public function up(): void { Schema::create('relationships', function (Blueprint $table) { $table->bigIncrements('id'); - $table->unsignedBigInteger('post_id'); - $table->unsignedBigInteger('another'); + $table->foreignId('post_id'); + $table->foreignId('another'); }); } diff --git a/tests/fixtures/migrations/identity-columns.php b/tests/fixtures/migrations/identity-columns.php index 3302c307..1c954920 100644 --- a/tests/fixtures/migrations/identity-columns.php +++ b/tests/fixtures/migrations/identity-columns.php @@ -13,8 +13,8 @@ public function up(): void { Schema::create('relationships', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('post_id'); - $table->unsignedBigInteger('another'); + $table->foreignId('post_id'); + $table->foreignId('another'); }); } diff --git a/tests/fixtures/migrations/indexes.php b/tests/fixtures/migrations/indexes.php index 953d93dc..f35fabe1 100644 --- a/tests/fixtures/migrations/indexes.php +++ b/tests/fixtures/migrations/indexes.php @@ -14,8 +14,8 @@ public function up(): void Schema::create('posts', function (Blueprint $table) { $table->unsignedInteger('id'); $table->string('title'); - $table->unsignedBigInteger('parent_post_id'); - $table->unsignedBigInteger('author_id'); + $table->foreignId('parent_post_id'); + $table->foreignId('author_id'); $table->timestamp('published_at')->nullable(); $table->unsignedInteger('word_count'); $table->geometry('location'); diff --git a/tests/fixtures/migrations/infer-belongsto.php b/tests/fixtures/migrations/infer-belongsto.php index 48ef2838..751828df 100644 --- a/tests/fixtures/migrations/infer-belongsto.php +++ b/tests/fixtures/migrations/infer-belongsto.php @@ -14,8 +14,8 @@ public function up(): void Schema::create('conferences', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->unsignedBigInteger('venue_id'); - $table->unsignedBigInteger('region_id'); + $table->foreignId('venue_id'); + $table->foreignId('region_id'); $table->timestamps(); }); } diff --git a/tests/fixtures/migrations/models-with-custom-namespace.php b/tests/fixtures/migrations/models-with-custom-namespace.php index ec10b74d..25bf9f3c 100644 --- a/tests/fixtures/migrations/models-with-custom-namespace.php +++ b/tests/fixtures/migrations/models-with-custom-namespace.php @@ -15,7 +15,7 @@ public function up(): void $table->id(); $table->string('name', 30); $table->string('image'); - $table->unsignedBigInteger('parent_id')->nullable(); + $table->foreignId('parent_id')->nullable(); $table->boolean('active')->default(true); $table->timestamps(); $table->softDeletes(); diff --git a/tests/fixtures/migrations/readme-example.php b/tests/fixtures/migrations/readme-example.php index 92ceba79..424e5699 100644 --- a/tests/fixtures/migrations/readme-example.php +++ b/tests/fixtures/migrations/readme-example.php @@ -16,7 +16,7 @@ public function up(): void $table->string('title', 400); $table->longText('content'); $table->timestamp('published_at')->nullable(); - $table->unsignedBigInteger('author_id'); + $table->foreignId('author_id'); $table->timestamps(); }); } diff --git a/tests/fixtures/migrations/relationships.php b/tests/fixtures/migrations/relationships.php index 0079985a..bfc37355 100644 --- a/tests/fixtures/migrations/relationships.php +++ b/tests/fixtures/migrations/relationships.php @@ -13,8 +13,8 @@ public function up(): void { Schema::create('comments', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('post_id'); - $table->unsignedBigInteger('author_id'); + $table->foreignId('post_id'); + $table->foreignId('author_id'); $table->timestamps(); }); } diff --git a/tests/fixtures/migrations/soft-deletes.php b/tests/fixtures/migrations/soft-deletes.php index c4881824..6ef4ec40 100644 --- a/tests/fixtures/migrations/soft-deletes.php +++ b/tests/fixtures/migrations/soft-deletes.php @@ -13,7 +13,7 @@ public function up(): void { Schema::create('comments', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('post_id'); + $table->foreignId('post_id'); $table->timestamps(); $table->softDeletes(); }); diff --git a/tests/fixtures/migrations/unconventional.php b/tests/fixtures/migrations/unconventional.php index a9131c87..cf0eea40 100644 --- a/tests/fixtures/migrations/unconventional.php +++ b/tests/fixtures/migrations/unconventional.php @@ -14,8 +14,8 @@ public function up(): void Schema::create('teams', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->unsignedBigInteger('owner'); - $table->unsignedBigInteger('manager'); + $table->foreignId('owner'); + $table->foreignId('manager'); $table->json('options'); $table->timestamps(); });