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:
+3
source to share
2 answers
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 to share