It's a foreign key so
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I've the next line on a migration method on upload or create table method:
$table->integerIncrements('storage_id')->index()->comment("FK(storages.id)");
But the column is created without index, and don't know whi What's the problem?
Solved by myself: The problem was here:
$table->unsignedInteger('content_id')->index()->unique()->comment("FK(contents.id)");
As you can see the new field can't be null, while I was trying to create constraint as:
$table->foreign('content_id')->references('id')->on('contents')->onDelete('set null')->onUpdate('cascade');
On delete set null is impossible, so the solution can be both:
$table->unsignedInteger('content_id')->index()->nullable()->unique()->comment("FK(contents.id)");
...
$table->foreign('content_id')->references('id')->on('contents')->onDelete('set null')->onUpdate('cascade');
Or forcing the deletion of children associated data:
$table->unsignedInteger('content_id')->index()->unique()->comment("FK(contents.id)");
...
$table->foreign('content_id')->references('id')->on('contents')->onDelete('cascade')->onUpdate('cascade');
I've finally applied the second one, due I don't want to mantain children data when the parent is deleted.
Please or to participate in this conversation.