Skip to content

Commit

Permalink
Align code and docs for unconventional foreign keys (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Jan 15, 2025
1 parent d0a7256 commit ae0b14a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,11 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
$column = Str::afterLast($column_name, '_');
} elseif (Str::contains($on, '.')) {
[$table, $column] = explode('.', $on);
$table = Str::snake($table);
} elseif (Str::contains($on, '\\')) {
$table = Str::lower(Str::plural(Str::afterLast($on, '\\')));
$table = Str::snake(Str::plural(Str::afterLast($on, '\\')));
$column = Str::afterLast($column_name, '_');
} else {
$table = Str::plural($on);
$table = Str::snake(Str::plural($on));
$column = Str::afterLast($column_name, '_');
}

Expand Down Expand Up @@ -315,12 +314,15 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
if ($on_delete_clause === 'cascade') {
$on_delete_suffix = '->cascadeOnDelete()';
}

if ($on_update_clause === 'cascade') {
$on_update_suffix = '->cascadeOnUpdate()';
}

if ($column_name === Str::singular($table) . '_' . $column) {
return self::INDENT . "{$prefix}->constrained(){$on_delete_suffix}{$on_update_suffix}";
}

if ($column === 'id') {
return self::INDENT . "{$prefix}->constrained('{$table}'){$on_delete_suffix}{$on_update_suffix}";
}
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/drafts/unconventional-foreign-key.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
models:
State:
name: string
countries_id: id foreign:countries
country_code: string foreign:countries
ccid: string foreign:countries
countries_id: id foreign:country
country_code: string foreign:Country
ccid: string foreign:country.id
c_code: string foreign:countries.code
2 changes: 1 addition & 1 deletion tests/fixtures/factories/unconventional-foreign-key.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function definition(): array
'name' => fake()->name(),
'countries_id' => Country::factory(),
'country_code' => Country::factory()->create()->code,
'ccid' => Country::factory()->create()->ccid,
'ccid' => Country::factory(),
'c_code' => Country::factory()->create()->code,
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/unconventional-foreign-key.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function up(): void
$table->string('country_code');
$table->foreign('country_code')->references('code')->on('countries');
$table->string('ccid');
$table->foreign('ccid')->references('ccid')->on('countries');
$table->foreign('ccid')->references('id')->on('country');
$table->string('c_code');
$table->foreign('c_code')->references('code')->on('countries');
$table->timestamps();
Expand Down

0 comments on commit ae0b14a

Please sign in to comment.