php – Laravel 5迁移标识符名称太长
发布时间:2020-12-14 19:41:19 所属栏目:大数据 来源:网络整理
导读:我正在尝试运行以下迁移: public function up(){ Schema::create('lifestyle_questions',function(Blueprint $table) { $table-increments('id'); $table-string('question'); $table-timestamps(); }); Schema::create('lifestyle_question_answers',funct
我正在尝试运行以下迁移:
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'); }); } 但是我收到以下错误: [IlluminateDatabaseQueryException] 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
搜索谷歌我发现
this
“你可以将一个自定义索引名称作为第二个参数传递到foreign()方法中,也可以使用较短的表/列名. 所以你想做一些类似的事情 public function up() { Schema::create('lifestyle_questions',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'); }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |