Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel migration primary (or key) "Identifier name is too long"

I have simple Laravel migration file specifying a composite primary key :

// ...

public function up()
{
    Schema::create('my_super_long_table_name', function($table)
    {
        $table->integer('column_1');
        $table->integer('column_2');
        $table->integer('column_3');

        $table->primary(['column_1', 'column_2', 'column_3']);
    });
}

// ...

And when running php artisan migrate this error is thrown :

SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'my_super_long_table_name_column_1_column_2_column_3' is too long
like image 289
Ifnot Avatar asked Feb 20 '15 10:02

Ifnot


1 Answers

Simply specify the key name when creating it (with the second argument for primary).

$table->primary(['column_1', 'column_2', 'column_3'], 'my_long_table_primary'); 

Next,

If you have error like You have an error in your SQL syntax ... after this modification please make sure you are not using reserved word by your database engine for your key name.

Eg for MySQL : http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

Tip : primary is reserved, so do not use it ;)

like image 77
Ifnot Avatar answered Sep 21 '22 23:09

Ifnot