diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 9f46c2b9..4aa56a75 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -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, '_'); } @@ -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}"; } diff --git a/tests/fixtures/drafts/unconventional-foreign-key.yaml b/tests/fixtures/drafts/unconventional-foreign-key.yaml index 7cfd1f2a..74c334b8 100644 --- a/tests/fixtures/drafts/unconventional-foreign-key.yaml +++ b/tests/fixtures/drafts/unconventional-foreign-key.yaml @@ -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 diff --git a/tests/fixtures/factories/unconventional-foreign-key.php b/tests/fixtures/factories/unconventional-foreign-key.php index e957cb98..26f2e995 100644 --- a/tests/fixtures/factories/unconventional-foreign-key.php +++ b/tests/fixtures/factories/unconventional-foreign-key.php @@ -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, ]; } diff --git a/tests/fixtures/migrations/unconventional-foreign-key.php b/tests/fixtures/migrations/unconventional-foreign-key.php index c6a44390..eeac6eb4 100644 --- a/tests/fixtures/migrations/unconventional-foreign-key.php +++ b/tests/fixtures/migrations/unconventional-foreign-key.php @@ -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();