Skip to content

Commit 46a2db9

Browse files
Use foreignId for consistency (#653)
1 parent 45f0e37 commit 46a2db9

13 files changed

+35
-25
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,7 @@ protected function buildDefinition(Model $model): string
165165
if ($column->name() === 'id' && $dataType === 'id') {
166166
$dataType = 'bigIncrements';
167167
} elseif ($dataType === 'id') {
168-
if ($model->isPivot()) {
169-
// TODO: what if constraints are enabled?
170-
$dataType = 'foreignId';
171-
} else {
172-
$dataType = 'unsignedBigInteger';
173-
}
168+
$dataType = 'foreignId';
174169
}
175170

176171
if (in_array($dataType, self::UNSIGNABLE_TYPES) && in_array('unsigned', $column->modifiers())) {

src/Lexers/ModelLexer.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private function buildModel(string $name, array $columns): Model
178178
if ($type === 'belongsTo') {
179179
$column = $this->columnNameFromRelationship($relationship);
180180
if (isset($columns[$column]) && !str_contains($columns[$column], ' foreign') && !str_contains($columns[$column], ' id')) {
181-
$columns[$column] .= ' id:' . Str::before($relationship, ':');
181+
$columns[$column] = trim($this->removeDataTypes($columns[$column]) . ' id:' . Str::before($relationship, ':'));
182182
}
183183
}
184184
}
@@ -215,7 +215,7 @@ private function buildColumn(string $name, string $definition): Column
215215
$data_type = null;
216216
$modifiers = [];
217217

218-
$tokens = preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition);
218+
$tokens = $this->parseColumn($definition);
219219
foreach ($tokens as $token) {
220220
$parts = explode(':', $token);
221221
$value = $parts[0];
@@ -330,4 +330,19 @@ private function hasBelongsToRelationship(Model $model, string $reference): bool
330330

331331
return false;
332332
}
333+
334+
private function removeDataTypes(string $definition): string
335+
{
336+
$tokens = array_filter(
337+
$this->parseColumn($definition),
338+
fn ($token) => strtolower($token) !== 'unsigned' && !isset(self::$dataTypes[strtolower($token)])
339+
);
340+
341+
return implode(' ', $tokens);
342+
}
343+
344+
private function parseColumn(string $definition): array
345+
{
346+
return preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition);
347+
}
333348
}

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public function it_infers_belongsTo_columns(): void
681681
$this->assertEquals([], $columns['id']->modifiers());
682682
$this->assertEquals('venue_id', $columns['venue_id']->name());
683683
$this->assertEquals('id', $columns['venue_id']->dataType());
684-
$this->assertEquals(['unsigned'], $columns['venue_id']->modifiers());
684+
$this->assertEquals([], $columns['venue_id']->modifiers());
685685
$this->assertEquals(['Venue'], $columns['venue_id']->attributes());
686686
$this->assertEquals('region_id', $columns['region_id']->name());
687687
$this->assertEquals('id', $columns['region_id']->dataType());

tests/fixtures/migrations/belongs-to-many.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up(): void
1414
Schema::create('journeys', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('name');
17-
$table->unsignedBigInteger('user_id');
17+
$table->foreignId('user_id');
1818
$table->timestamps();
1919
});
2020
}

tests/fixtures/migrations/identity-columns-big-increments.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public function up(): void
1313
{
1414
Schema::create('relationships', function (Blueprint $table) {
1515
$table->bigIncrements('id');
16-
$table->unsignedBigInteger('post_id');
17-
$table->unsignedBigInteger('another');
16+
$table->foreignId('post_id');
17+
$table->foreignId('another');
1818
});
1919
}
2020

tests/fixtures/migrations/identity-columns.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public function up(): void
1313
{
1414
Schema::create('relationships', function (Blueprint $table) {
1515
$table->id();
16-
$table->unsignedBigInteger('post_id');
17-
$table->unsignedBigInteger('another');
16+
$table->foreignId('post_id');
17+
$table->foreignId('another');
1818
});
1919
}
2020

tests/fixtures/migrations/indexes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function up(): void
1414
Schema::create('posts', function (Blueprint $table) {
1515
$table->unsignedInteger('id');
1616
$table->string('title');
17-
$table->unsignedBigInteger('parent_post_id');
18-
$table->unsignedBigInteger('author_id');
17+
$table->foreignId('parent_post_id');
18+
$table->foreignId('author_id');
1919
$table->timestamp('published_at')->nullable();
2020
$table->unsignedInteger('word_count');
2121
$table->geometry('location');

tests/fixtures/migrations/infer-belongsto.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function up(): void
1414
Schema::create('conferences', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('name');
17-
$table->unsignedBigInteger('venue_id');
18-
$table->unsignedBigInteger('region_id');
17+
$table->foreignId('venue_id');
18+
$table->foreignId('region_id');
1919
$table->timestamps();
2020
});
2121
}

tests/fixtures/migrations/models-with-custom-namespace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up(): void
1515
$table->id();
1616
$table->string('name', 30);
1717
$table->string('image');
18-
$table->unsignedBigInteger('parent_id')->nullable();
18+
$table->foreignId('parent_id')->nullable();
1919
$table->boolean('active')->default(true);
2020
$table->timestamps();
2121
$table->softDeletes();

tests/fixtures/migrations/readme-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function up(): void
1616
$table->string('title', 400);
1717
$table->longText('content');
1818
$table->timestamp('published_at')->nullable();
19-
$table->unsignedBigInteger('author_id');
19+
$table->foreignId('author_id');
2020
$table->timestamps();
2121
});
2222
}

tests/fixtures/migrations/relationships.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public function up(): void
1313
{
1414
Schema::create('comments', function (Blueprint $table) {
1515
$table->id();
16-
$table->unsignedBigInteger('post_id');
17-
$table->unsignedBigInteger('author_id');
16+
$table->foreignId('post_id');
17+
$table->foreignId('author_id');
1818
$table->timestamps();
1919
});
2020
}

tests/fixtures/migrations/soft-deletes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function up(): void
1313
{
1414
Schema::create('comments', function (Blueprint $table) {
1515
$table->id();
16-
$table->unsignedBigInteger('post_id');
16+
$table->foreignId('post_id');
1717
$table->timestamps();
1818
$table->softDeletes();
1919
});

tests/fixtures/migrations/unconventional.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function up(): void
1414
Schema::create('teams', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('name');
17-
$table->unsignedBigInteger('owner');
18-
$table->unsignedBigInteger('manager');
17+
$table->foreignId('owner');
18+
$table->foreignId('manager');
1919
$table->json('options');
2020
$table->timestamps();
2121
});

0 commit comments

Comments
 (0)