-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Calling ->index()
after constrained()
sets foreign key name to 1
#53768
Comments
->index()
after constrained()
sets index name to 1
->index()
after constrained()
sets foreign key name to 1
Wouldn't using |
It adds a foreign key, but in postgres, i don't think a foreign key is an index. In mysql and mariadb it also works as an index (or it adds an index as well when adding foreign keys). As per https://stackoverflow.com/a/970605/1223692 (although not particularly new that SO thread.) Either way, it is the naming of said index that has a weird behaviour here |
Yes it seems weird, but calling <?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('foos', function (Blueprint $table) {
$table->ulid('id')->primary();
});
Schema::create('example', function (Blueprint $table) {
tap($table->foreignUlid('foo_id')->nullable(), function (ForeignIdColumnDefinition $column) {
$column->constrained('foos')->nullOnDelete();
})->index();
});
}
} |
Laravel Version
11.34.2
PHP Version
8.4.1
Database Driver & Version
Postgres 16
Description
In migrations, chaining
->index()
on aforeignId()
,foreignUlid()
orforeignUuid()
column after->constrained()
, will lead to the foreign key being named1
instead of<table_name>_<column_name>_foreign
as expected.Steps To Reproduce
In a fresh application, if I add a migration that contains this:
The foreign key is just named
1
.If I change the migration and call
->index()
right afterforeignUlid()
instead, the foreign key is namedexample_foo_id_foreign
instead. Example:I have only tested this with Postgres, not sure if it affects MySQL or other DB types.
The text was updated successfully, but these errors were encountered: