Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
$on_update_suffix = '->cascadeOnUpdate()';
}

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

Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,43 @@ public function output_generates_custom_pivot_tables(): void
$this->assertEquals(['created' => [$user_migration, $team_migration, $pivot_migration]], $this->subject->output($tree));
}

#[Test]
public function output_creates_constraint_for_foreign_key_to_non_id_column_with_underscore(): void
{
$this->app->config->set('blueprint.use_constraints', true);

$this->filesystem->expects('stub')
->with('migration.stub')
->andReturn($this->stub('migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$agent_path = str_replace(
'timestamp',
$now->copy()->subSecond()->format('Y_m_d_His'),
'database/migrations/timestamp_create_agents_table.php'
);

$employment_path = str_replace(
'timestamp',
$now->format('Y_m_d_His'),
'database/migrations/timestamp_create_employments_table.php'
);

$this->filesystem->expects('exists')->times(2)->andReturn(false);

$this->filesystem->expects('put')
->with($agent_path, $this->fixture('migrations/foreign-key-underscore-agents.php'));
$this->filesystem->expects('put')
->with($employment_path, $this->fixture('migrations/foreign-key-underscore.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/foreign-key-underscore.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$agent_path, $employment_path]], $this->subject->output($tree));
}

public static function modelTreeDataProvider()
{
return [
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/drafts/foreign-key-underscore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
models:

Agent:
id: false
party_id: id

Employment:
id: false
# Referencing agents.party_id (custom column, has underscore)
agent_party_id: id foreign:agents.party_id
31 changes: 31 additions & 0 deletions tests/fixtures/migrations/foreign-key-underscore-agents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::disableForeignKeyConstraints();

Schema::create('agents', function (Blueprint $table) {
$table->foreignId('party_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('agents');
}
};
31 changes: 31 additions & 0 deletions tests/fixtures/migrations/foreign-key-underscore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::disableForeignKeyConstraints();

Schema::create('employments', function (Blueprint $table) {
$table->foreignId('agent_party_id')->constrained('agents', 'party_id')->cascadeOnDelete()->cascadeOnUpdate();
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('employments');
}
};
Loading