Laravel 5 - Component Key Indexes on Migration

Composite indexes are a subject that I am not fully aware of, I was not sure if I was doing it right? Or if Laravel parses my code correctly when porting. Does it look right?

Schema::create('friends', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('requester_id')->unsigned();
    $table->integer('requestee_id')->unsigned();
    $table->timestamps();

    $table->foreign('requester_id')->references('id')->on('users');
    $table->foreign('requestee_id')->references('id')->on('users');

    $table->unique(['requester_id', 'requestee_id'], 'composite_index');
});

      

Here's what Sequel Pro shows:

http://i.imgur.com/5A4LZH3.png

+3


source to share


2 answers


What you have is correct.




Note : you do not need to provide an index name. Laravel will automatically generate a name for you based on the indexed columns.

+4


source


Here's my example, making it unique external_id

for each user user_id

:



    Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');

        $table->string('external_id')->nullable();
        $table->unique(['external_id', 'user_id']); // <<---------

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

        // ...
    });

      

+1


source







All Articles