Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Allow specifying index name when calling ForeignIdColumnDefinition@constrained() #46746

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Illuminate/Database/Schema/ForeignIdColumnDefinition.php
Expand Up @@ -31,22 +31,24 @@ public function __construct(Blueprint $blueprint, $attributes = [])
* Create a foreign key constraint on this column referencing the "id" column of the conventionally related table.
*
* @param string|null $table
* @param string $column
* @param string|null $column
* @param string|null $indexName
* @return \Illuminate\Database\Schema\ForeignKeyDefinition
*/
public function constrained($table = null, $column = 'id')
public function constrained($table = null, $column = 'id', $indexName = null)
{
return $this->references($column)->on($table ?? Str::of($this->name)->beforeLast('_'.$column)->plural());
return $this->references($column, $indexName)->on($table ?? Str::of($this->name)->beforeLast('_'.$column)->plural());
}

/**
* Specify which column this foreign ID references on another table.
*
* @param string $column
* @param string $indexName
* @return \Illuminate\Database\Schema\ForeignKeyDefinition
*/
public function references($column)
public function references($column, $indexName = null)
{
return $this->blueprint->foreign($this->name)->references($column);
return $this->blueprint->foreign($this->name, $indexName)->references($column);
}
}
11 changes: 11 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Expand Up @@ -497,6 +497,17 @@ public function testAddingForeignID()
], $statements);
}

public function testAddingForeignIdSpecifyingIndexNameInConstraint()
{
$blueprint = new Blueprint('users');
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertSame([
'alter table `users` add `company_id` bigint unsigned not null',
'alter table `users` add constraint `my_index` foreign key (`company_id`) references `companies` (`id`)',
], $statements);
}

public function testAddingBigIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Expand Up @@ -416,6 +416,17 @@ public function testAddingForeignID()
], $statements);
}

public function testAddingForeignIdSpecifyingIndexNameInConstraint()
{
$blueprint = new Blueprint('users');
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertSame([
'alter table "users" add column "company_id" bigint not null',
'alter table "users" add constraint "my_index" foreign key ("company_id") references "companies" ("id")',
], $statements);
}

public function testAddingBigIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Expand Up @@ -321,6 +321,16 @@ public function testAddingForeignID()
], $statements);
}

public function testAddingForeignIdSpecifyingIndexNameInConstraint()
{
$blueprint = new Blueprint('users');
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertSame([
'alter table "users" add column "company_id" integer not null',
], $statements);
}

public function testAddingBigIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Expand Up @@ -366,6 +366,17 @@ public function testAddingForeignID()
], $statements);
}

public function testAddingForeignIdSpecifyingIndexNameInConstraint()
{
$blueprint = new Blueprint('users');
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertSame([
'alter table "users" add "company_id" bigint not null',
'alter table "users" add constraint "my_index" foreign key ("company_id") references "companies" ("id")',
], $statements);
}

public function testAddingBigIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down