RadicalActivity's avatar

Laravel 9 Migration not creating Foreign Key

I just have a fresh Laravel 9 (latest) installation. I have made some migrations successfully and tried to add foreign key constraints, but Laravel's not creating the actual Foreign Key itself. It doesn't throw any errors and I can query everything just fine, but no Foreign key is being created.

What's strange is that I've simply copied the Migration code from one of my older projects. Here is the 2 migration Schema:

Schema::create('users', function (Blueprint $table)
{
    $table->id();
    $table->string('name');
    $table->timestamps();
});

Schema::create('posts', function (Blueprint $table)
{
    $table->id();
    $table->foreignId('user_id')->constrained();
    $table->string('name');
    $table->timestamps();
});

When I do the migration I get an index called posts_user_id_foreign but no Foreign Key is being added.

I have already tried the following variations on the Posts table with no success:

$table->foreignId('user_id')->constrained('users');
-----
$table->foreignId('user_id')->constrained('users')->onUpdate('cascade');
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
-----
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');

Can anyone please tell me why is this happening and what's the solution? I had no issues creating Foreign Key before with Laravel. Not sure what's changed now in the latest version...

0 likes
6 replies
RadicalActivity's avatar

@tykus I am using MariaDB 10.6.5. As far as I know, foreign keys are enabled. I have successfully used them in many of my other projects. I have tested the above config again on a fresh Laravel installation and I get the same problem.

RadicalActivity's avatar

Can anyone please check a fresh new installation and see if they get the same issue? It seems to be a bug to me. No matter what I do I never get the foreign keys created.

georgecoffey's avatar

I'm using PostgreSQL and seem to be having the same problem. Haven't figure it out yet. I guess I don't really need the foreign keys as they seem to exist within laravel now? But they don't show up in the database

Nesster's avatar

I'm using PostgreSQL too and just noticed this as well. But if I specify ->references('id')->on('table') it creates the foreign key.

Like this: $table->foreignId('user_id')->references('id')->on('users');

MaheshBroDev's avatar

This also happened to me, Migrations ran OK. Tables were created. Data Inserted. No issues on any of those. But foreign keys were not being created. Well, it was because I had not updated my database config file accurately.

even though I've updated the ".env" file with correct connection data I had not updated the database config file to use mysql as the default driver. It was set as the "sqlite". Updated it, Now it works.

Also, If this still does not solve your problem then it could be that the database engine does not support the foreign key relations. You may need to change the "engine" property of the mysql config to "InnoDB" (default is null in the config file) to see if it fixes the issue. Cheers!! Happy coding.

Please or to participate in this conversation.