Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sebastiansulinski's avatar

Migration multiple foreign keys

I'm just realised that even though I've set two foreign keys in my migration only the second one gets actually added.

Here's my schema:

Schema::create('favorites', function (Blueprint $table) {

    $table->integer('lecture_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->timestamps();

    $table->primary(['lecture_id', 'user_id']);

    $table->foreign('lecture_id')
          ->references('id')->on('lectures')
          ->onDelete('cascade');

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

});

I get both primary keys and only watched_user_id_foreign for the foreign constraint. Any idea why?

0 likes
4 replies
bobbybouwmann's avatar
Level 88

It's working fine for me!

I get the following results

Structure

Relations

1 like
bobbybouwmann's avatar

As you can see on the image! The tests_user_id_foreign is an index, not a foreign key ;)

Please or to participate in this conversation.