Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make column unique and index it in Laravel Migration?

I would like to alter my one of my column on my table - and make them a unique index.

I tried :

public function up()
{
    Schema::table('inventories', function($table)
    {           
        $table->string('sku',255)->unique();
    });
}

I get :

enter image description here

What is the most efficient way to do that ?

like image 292
iori Avatar asked Mar 04 '15 16:03

iori


2 Answers

You can add indexes separately from their definition. As documented here

Schema::table('inventories', function($table)
{
    $table->unique('sku');
});
like image 77
lukasgeiter Avatar answered Nov 03 '22 05:11

lukasgeiter


I got it.

I did this :

public function up()
{           
    DB::update('alter table `inventories` modify `sku` VARCHAR(200) UNIQUE NOT NULL');
}

and now It works :

enter image description here

like image 21
iori Avatar answered Nov 03 '22 04:11

iori