Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 migrations Schema - index length

I ran into a problem with laravel migration where I need to set index length for a specific column but it looks like Schema/Blueprint index() does not have such feature. Laravel Docs

      [Illuminate\Database\QueryException]                                         
      SQLSTATE[42000]: Syntax error or access violation: 
      1170 BLOB/TEXT column 'description' used in key specification 
      without a key length (SQL: alter table `Customers` 
      add index `description_idx`(`description`)) 

Original sql query line:

KEY `description_idx` (`description`(100)) // index length = 100

Laravel migration code line:

$table->text('description')->nullable()->index('`description_idx`'); // no index length here

At moment I feel the best I can do is to change column type, but maybe there is more appropriate way to fix this issue?

like image 825
mintaras Avatar asked Jul 17 '14 00:07

mintaras


Video Answer


1 Answers

You can create these indexes manually using DB::statement(); in your migration.

In your up() do something like this

DB::statement('CREATE INDEX description_idx ON Customers (description(100));');

And then in your down() you can simply

Schema::table('Customers', function($table) {
    $table->dropIndex('description_idx');
});

Hope that helps.

like image 88
judereid Avatar answered Oct 11 '22 13:10

judereid