Skip to content

Commit 9e8bd03

Browse files
authored
Allow non-id columns to be nullable when using foreign (#486)
1 parent e446a41 commit 9e8bd03

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ public function output(Tree $tree, $overwrite = false): array
6363
$tables = ['tableNames' => [], 'pivotTableNames' => []];
6464

6565
$stub = $this->filesystem->stub('migration.stub');
66-
6766
/**
68-
* @var \Blueprint\Models\Model $model
69-
*/
67+
* @var \Blueprint\Models\Model $model
68+
*/
7069
foreach ($tree->models() as $model) {
7170
$tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model);
7271

@@ -98,7 +97,6 @@ protected function createMigrations(array $tables, $overwrite = false): array
9897
$path = $this->getTablePath($tableName, $sequential_timestamp->addSecond(), $overwrite);
9998
$action = $this->filesystem->exists($path) ? 'updated' : 'created';
10099
$this->filesystem->put($path, $data);
101-
102100
$output[$action][] = $path;
103101
}
104102

@@ -109,7 +107,6 @@ protected function createMigrations(array $tables, $overwrite = false): array
109107

110108
$output[$action][] = $path;
111109
}
112-
113110
return $output;
114111
}
115112

@@ -208,12 +205,12 @@ protected function buildDefinition(Model $model)
208205

209206
// TODO: unset the proper modifier
210207
$modifiers = collect($modifiers)->reject(
211-
function ($modifier) {
208+
function ($modifier) use ($column) {
212209
return (is_array($modifier) && key($modifier) === 'foreign')
213210
|| (is_array($modifier) && key($modifier) === 'onDelete')
214211
|| (is_array($modifier) && key($modifier) === 'onUpdate')
215212
|| $modifier === 'foreign'
216-
|| ($modifier === 'nullable' && $this->isLaravel7orNewer());
213+
|| ($modifier === 'nullable' && $this->isLaravel7orNewer() && $column->dataType() === 'id');
217214
}
218215
);
219216
}

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,33 @@ public function output_creates_foreign_keys_with_nullable_chained_correctly()
643643
$this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree));
644644
}
645645

646+
/**
647+
* @test
648+
*/
649+
public function output_creates_nullable_foreign_key_without_column_type_beeing_id()
650+
{
651+
$this->filesystem->expects('stub')
652+
->with('migration.stub')
653+
->andReturn($this->stub('migration.stub'));
654+
655+
$now = Carbon::now();
656+
Carbon::setTestNow($now);
657+
658+
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_comments_table.php');
659+
660+
$this->filesystem->expects('exists')->with($model_migration)->andReturn(false);
661+
662+
$this->files
663+
->expects('put')
664+
->with($model_migration, $this->fixture('migrations/nullable-columns-with-foreign.php'));
665+
666+
$tokens = $this->blueprint->parse($this->fixture('drafts/nullable-columns-with-foreign.yaml'));
667+
668+
$tree = $this->blueprint->analyze($tokens);
669+
670+
$this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree));
671+
}
672+
646673
/**
647674
* @test
648675
* @environment-setup useLaravel6
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
models:
2+
Comment:
3+
id
4+
user_id: id foreign:users.id nullable
5+
integer: integer foreign:monsteras.id nullable
6+
timestamps
7+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::disableForeignKeyConstraints();
17+
18+
Schema::create('comments', function (Blueprint $table) {
19+
$table->id();
20+
$table->foreignId('user_id')->nullable()->constrained();
21+
$table->integer('integer')->nullable();
22+
$table->foreign('integer')->references('id')->on('monsteras');
23+
$table->timestamps();
24+
});
25+
26+
Schema::enableForeignKeyConstraints();
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('comments');
37+
}
38+
}

0 commit comments

Comments
 (0)