Laravel 5迁移标识符名称太长

Posted

技术标签:

【中文标题】Laravel 5迁移标识符名称太长【英文标题】:Laravel 5 migration identifier name too long 【发布时间】:2015-07-22 02:39:35 【问题描述】:

我正在尝试运行以下迁移:

public function up()

    Schema::create('lifestyle_questions', function(Blueprint $table)
    
        $table->increments('id');
        $table->string('question');
        $table->timestamps();
    );

    Schema::create('lifestyle_question_answers', function(Blueprint $table)
    
        $table->increments('id');
        $table->integer('lifestyle_question_id')->unsigned();
        $table->foreign('lifestyle_question_id')->references('id')->on('lifestyle_questions');
        $table->string('answer');
        $table->timestamps();
    );

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table)
    
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('lifestyle_question_answer_id')->unsigned();
        $table->foreign('lifestyle_question_answer_id')->references('id')->on('lifestyle_question_answers');
    );

但我收到以下错误:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long (SQL: alter table `user_lifestyle_question_answers` add constraint user_lifestyle_question_answers_lifestyle_question_answer_id_foreign foreign key (`lifestyle_question_answer_id`) references `lifestyle_question_answers` (`id`))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long

【问题讨论】:

【参考方案1】:

您可以将自定义索引名称作为第二个参数传递给 foreign() 方法。或者只使用较短的表/列名。

所以你想做这样的事情:

public function up()

    Schema::create('lifestyle_questions', function(Blueprint $table)
    
        $table->increments('id');
        $table->string('question');
        $table->timestamps();
    );

    Schema::create('lifestyle_question_answers', function(Blueprint $table)
    
        $table->increments('id');
        $table->integer('lifestyle_question_id')->unsigned();
        $table->foreign('lifestyle_question_id', 'lq_id_foreign')->references('id')->on('lifestyle_questions');
        $table->string('answer');
        $table->timestamps();
    );

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table)
    
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('lifestyle_question_answer_id')->unsigned();
        $table->foreign('lifestyle_question_answer_id', 'lqa_id_foreign')->references('id')->on('lifestyle_question_answers');
    );

【讨论】:

仅供参考:根据dev.mysql.com/doc/refman/5.5/en/identifiers.html,MySQL 标识符应始终小于 64 个字符 很遗憾,此解决方案在生成复合键时不适用于primary() 方法:$table->primary(['id1', 'id'], 'custom_index'); 如何解决此问题? 对于那些在生成组合键为@Dong3000 时遇到此问题的人,请参阅:***.com/a/28626907/3368784 对于使用foreignId的人,您可以像这样使用它:$table->foreignId('really_long_foreign_key_id')->constrained()->index('short_id_foreign');

以上是关于Laravel 5迁移标识符名称太长的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 迁移错误:语法错误或访问冲突:1071 指定的键太长;最大密钥长度为 767 字节 laravel 5.3

mariadb 上的学说行为可翻译索引标识符太长

Oracle PLS-00114: 标识符 ' ' 太长

Laravel 迁移命名约定

在LINUX内核中,进程标识符PID为1,2 ,3,4,5的进程的名称是啥?基本功能是啥?

Oracle DB - 标识符太长