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

我怕爱的太早我们不能终老 提交于 2019-12-22 01:33:33

问题


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

回答1:


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 ;)



来源:https://stackoverflow.com/questions/28626906/laravel-migration-primary-or-key-identifier-name-is-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!