简体   繁体   中英

Laravel 5.1 foreign key migration error

I get this weird error when executing my constraints migration When I execute this migration I get

 [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)

I've read that the data types in different tables should match ( like int(10) to int(10)) and they do, this is also the last migration that I have ( all the tables exist and have been created by other migrations before).

I seriously have no idea what to do anymore :(. Could someone help? I have seen similar posts, but they have not fixed the problem so far for me.

The migration is as follows:

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

    }

In the beginning of your migration turn off foreign key constraints.

SET FOREIGN_KEY_CHECKS=0;

Then, when the data is migrated, turn them back on.

SET FOREIGN_KEY_CHECKS=1;

If that doesn't work, then try adding the following line.

$table->engine = 'InnoDB'

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

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

Try to use two separate migrations.

Create first migration, which will create tables and all columns (put everything except $table->foreign clauses here). Run migrate command. Do not execute second one, temporarily remove it from migrations directory, if you've already have this migration.

Then create second migration, which will add foreign keys to already created columns (use only $table->foreign clauses here). Run migrate command again.

This helped me, when I was in the same situation. I hope it will work for you too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM