【问题标题】:Laravel migration primary (or key) "Identifier name is too long"Laravel 迁移主(或键)“标识符名称太长”
【发布时间】:2015-04-22 00:13:48
【问题描述】:

我有一个简单的 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']);
    });
}

// ...

在运行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

【问题讨论】:

  • 如果您可以指定要从中迁移的确切版本以及要迁移到的版本,这可能会有所帮助。
  • laravel 中的迁移是一个定义数据库结构的文件。不是从一个版本到另一个应用程序的“真正”迁移过程。

标签: mysql laravel migration


【解决方案1】:

创建时只需指定键名(第二个参数为primary)。

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

接下来,

如果您在此修改后出现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');
  • 在模型中定义关系时,是否也需要指定FK名称?
猜你喜欢
  • 2015-07-22
  • 2022-11-17
  • 2019-02-14
  • 1970-01-01
  • 2014-07-10
  • 2012-10-19
  • 2019-05-17
  • 2013-03-04
  • 2012-01-18
相关资源
最近更新 更多