Skip to content

Commit 07a0855

Browse files
authored
Fix migration for FQCN references (#503)
1 parent 5d14631 commit 07a0855

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
294294
} elseif (Str::contains($on, '.')) {
295295
[$table, $column] = explode('.', $on);
296296
$table = Str::snake($table);
297+
} elseif (Str::contains($on, '\\')) {
298+
$table = Str::lower(Str::plural(Str::afterLast($on, '\\')));
299+
$column = Str::afterLast($column_name, '_');
297300
} else {
298301
$table = Str::plural($on);
299302
$column = Str::afterLast($column_name, '_');

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ public function modelTreeDataProvider()
604604
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],
605605
['drafts/columns-with-comments.yaml', 'database/migrations/timestamp_create_professions_table.php', 'migrations/columns-with-comments.php'],
606606
['drafts/boolean-column-default.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/boolean-column-default.php'],
607+
['drafts/foreign-with-class.yaml', 'database/migrations/timestamp_create_events_table.php', 'migrations/foreign-with-class.php'],
607608
];
608609
}
609610
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
models:
2+
Event:
3+
working_title: string
4+
image_id: uuid foreign:\Tests\Models\Attachment nullable
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateEventsTable 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('events', function (Blueprint $table) {
19+
$table->id();
20+
$table->string('working_title');
21+
$table->foreignUuid('image_id')->nullable()->constrained('attachments');
22+
$table->timestamps();
23+
});
24+
25+
Schema::enableForeignKeyConstraints();
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::dropIfExists('events');
36+
}
37+
}

0 commit comments

Comments
 (0)