Laravel迁移主要(或密钥)"标识符名称太长"

Ifn*_*not 22 mysql migration laravel

我有简单的Laravel迁移文件,用于指定复合主键:

// ...

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']);
    });
}

// ...
Run Code Online (Sandbox Code Playgroud)

并且在运行时php artisan migrate抛出此错误:

SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'my_super_long_table_name_column_1_column_2_column_3' is too long
Run Code Online (Sandbox Code Playgroud)

Ifn*_*not 44

只需在创建时指定密钥名称(使用第二个参数primary).

$table->primary(['column_1', 'column_2', 'column_3'], 'my_long_table_primary');
Run Code Online (Sandbox Code Playgroud)

下一个,

如果您You have an error in your SQL syntax ...在此修改后遇到错误,请确保您的数据库引擎没有使用保留字作为您的密钥名称.

例如,对于MySQL:http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

提示:primary是保留的,所以不要使用它;)

  • 太好了!它也适用于外键`$ table-> foreign('foreign_column','fk_name') - > references('id') - > on('foreign_table');`. (8认同)