【问题标题】:Laravel 5.1 foreign key migration errorLaravel 5.1 外键迁移错误
【发布时间】:2016-07-06 23:59:01
【问题描述】:

执行约束迁移时出现这个奇怪的错误 当我执行此迁移时,我得到

 [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121) (SQL: alter table `questions` add constraint questions_inventory_i
  d_foreign foreign key (`inventory_id`) references `inventories` (`id`) on delete cascade)



  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121)

我已经读到不同表中的数据类型应该匹配(例如 int(10) 到 int(10))并且它们确实匹配,这也是我拥有的最后一次迁移(所有表都存在并且已经创建之前的其他迁移)。

我真的不知道该怎么办了:(。有人可以帮忙吗? 我看过类似的帖子,但到目前为止他们还没有为我解决问题。

迁移如下:

public function up()
    {
        Schema::table('questions', function ($table) {
            $table->integer('inventory_id')->unsigned()->change();
            $table->foreign('inventory_id')
                ->references('id')->on('inventories')
                ->onDelete('cascade');  
        });

        Schema::table('question_response', function ($table) {
            $table->integer('question_id')->unsigned()->change();
            $table->foreign('question_id')
                ->references('id')->on('questions')
                ->onDelete('cascade');
        });

        Schema::table('question_response', function ($table) {
            $table->integer('sample_result_set_id')->unsigned()->change();             
            $table->foreign('sample_result_set_id')
                ->references('id')->on('sample_response_set')
                ->onDelete('cascade');
        });

        Schema::table('inventory_response', function ($table) {
            $table->integer('inventory_id')->unsigned()->change();  
            $table->foreign('inventory_id')
                ->references('id')->on('inventories')
                ->onDelete('cascade');
        });

        Schema::table('inventory_response', function ($table) {
            $table->integer('sample_result_set_id')->unsigned()->change(); 
            $table->foreign('sample_result_set_id')
                ->references('id')->on('sample_response_set')
                ->onDelete('cascade');
        });

    }

【问题讨论】:

  • 您使用的是哪个数据库?
  • @AaronFranco MySql

标签: php mysql laravel migration laravel-5.1


【解决方案1】:

在迁移开始时关闭外键约束。

SET FOREIGN_KEY_CHECKS=0;

然后,当迁移数据时,重新打开它们。

SET FOREIGN_KEY_CHECKS=1;

如果这不起作用,请尝试添加以下行。

$table->engine = 'InnoDB'

Further, you can rename the constraint since the name may already be used...

$table->foreign('inventory_id', 'fk_questions_inventory_id')->...

【讨论】:

  • 添加了 DB::statement('SET FOREIGN_KEY_CHECKS = 0');之前和 DB::statement('SET FOREIGN_KEY_CHECKS = 1');迁移结束后@AaronFranco
  • innoDB 没有帮助@AaronFranco
【解决方案2】:

尝试使用两个单独的迁移。

创建第一个迁移,它将创建表和所有列(将除$table->foreign 子句之外的所有内容放在此处)。运行migrate 命令。不要执行第二个,暂时将其从迁移目录中删除,如果您已经进行了此迁移。

然后创建第二个迁移,这会将外键添加到已创建列(此处仅使用$table->foreign 子句)。再次运行migrate 命令。

当我处于同样的情况时,这对我很有帮助。我希望它也对你有用。

【讨论】:

  • 主要问题是我很笨,一些迁移​​已经运行,这意味着它每次都失败,我花了一些时间才意识到这一点。谢谢你的帮助:)
猜你喜欢
  • 2016-02-10
  • 1970-01-01
  • 2019-01-31
  • 2014-09-27
  • 2018-09-22
  • 2015-08-14
  • 2016-03-11
  • 2015-03-02
  • 2019-02-09
相关资源
最近更新 更多