laravel errno 150 foreign key constraint is incorrectly formed

北慕城南 提交于 2019-12-29 04:43:07

问题


Can anybody help me to solve this problem?

There are 3 tables with 2 foreign keys:

         Schema::create('users', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name');
                    $table->string('email')->unique();
                    $table->string('password');
                    $table->rememberToken();
                    $table->timestamps();
                });

        Schema::create('firms', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('title')->nullable();  
                    $table->integer('user_id')->unsigned()->nullable();
                    $table->foreign('user_id')->references('id')->on('users');
                    $table->timestamps();
                });
       Schema::create('jobs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title')->nullable();
            $table->integer('firm_id')->unsigned()->nullable();
            $table->foreign('firm_id')->references('id')->on('firms');
            $table->timestamps();
        });

Error after running migration:

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `job`.`#sql-5fc_a1`
   (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter ta
  ble `firms` add constraint `firms_user_id_foreign` foreign key (`user_id`)
  references `users` (`id`))

  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `job`.`#sql-5fc_a1`
   (errno: 150 "Foreign key constraint is incorrectly formed")

回答1:


In case of foreign keys, the referenced and referencing fields must have exactly the same data type.

You create the id fields in both users and firms as signed integers. However, you create both foreign keys as unsigned integers, therefore the creation of the keys fail.

You need to either add the unsigned clause to the id field definitions, or remove the unsigned clause from the foreign key fields.




回答2:


This answer is not better than the six answers before it but it is a more comprehensive answer on what causes laravel-errno-150-foreign-key-constraint-is-incorrectly-formed and how to fix specifically for laravel.

1) Spelling : often at times a wrong spelling of the referenced column name or referenced table name can throw up this error and you won't know as the error trace is not very descriptive.

2) Unique : the referenced column must be unique or indexed either by adding ->primary() or adding ->unique() or adding ->index() to the column definition in your migration.

3) Data type : the referenced and referencing fields must have exactly the same data type. this can not be stressed enough.

  • for bigincrements expected data type is bigInteger('column_name')->unsigned();

  • for increments expected is integer('column_name')->unsigned(); etc.

4) Remnants : when this error occurs it does not mean that the table is not migrated rather it is migrated but the foreign key columns are not set and it is not added to the migration table hence running php artisan migrate:reset will remove other tables except the faulty tables, so a manual drop of the faulty table is recommended to avoid further errors.

5) Order : this is often the most usual cause of this error the table being referenced must be created or migrated before the reference table else artisan wont find where to integrate the foreign key. to ensure an order for the migration process rename the migration file example:

  • Table A:2014_10_12_000000_create_users_table.php and
  • Table B:2014_10_12_100000_create_password_resets_table.php

This indicates that Table A will always come before Table B to change that, i will rename Table B to 2014_10_11_100000_create_password_resets_table.php now it will migrate before Table A.

6) Enable Foreign Key : if all else fails then add Schema::enableForeignKeyConstraints(); inside your function up() before your migration code example:

class CreateUsersTable extends Migration
{
 /**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
    Schema::enableForeignKeyConstraints();
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
    Schema::dropIfExists('users');
 }
}

To read more see laravel foreign key and laravel migrations

Mention any more fixes that i missed in the comments thanks.




回答3:


Most of the time this kind of error occurs due to the datatype mismatch on both the table.

Both primary key table & foreign key table should use same datatype and same option.

For example:

users

        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

orders

 Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('products');
            $table->bigInteger('user_id')->unsigned();
           $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->timestamp('added_on');
        });

On above example, I am trying to assign foreign key to users table from order table but I have bigInteger datatable in order table while in user table, I have simple integer. That's why it's generated this kind of error.

Also, If you have used unsigned(), nullable() etc options with primary or foreign key then you should use same at both the place.




回答4:


  • users
  • cashier refers users
  • student refers cashier

In addition when declaring foreign keys in laravel all tables your are referring must be on the top. In this case you can use "->unsigned()" modifier..




回答5:


we are table villes:

Schema::create('villes', function (Blueprint $table) {
            $table->bigIncrements('idVille'); // bigIncrement(8 bits)
            $table->string('nomVille');
            $table->timestamps();
        });

foreign key in table users for exemple :

Schema::table('users', function (Blueprint $table) {
            $table->bigInteger('ville_idVille')->unsigned()->after('tele');
            $table->foreign('ville_idVille')->references('idVille')->on('villes');
        });



回答6:


For PHP laravel 5.8 use unsigned modifier in this format

$table->unsignedBigInteger('user_id');

drop all tables in the database and run the migration again




回答7:


This happens when you test your laravel app and you have used an instance of MySQL in your tests that is subject to database migrations.

Prior to running the test, my migrations work fine. Then I encountered this error.

The way I resolved this issue was to simply drop the whole database and re-create it. Then migrate again.




回答8:


if you set referencing fields and be have exactly the same data type but error exists you can change date migration file just its working




回答9:


If the reference table primary key is in BigIcrements then Use the BigInteger for foreign key also like below

Table ATable

public function up()
{
    Schema::create('a_tables', function (Blueprint $table) {
        $table->bigIncrements('id');
   }
}

TABLE BTable

public function up()
{
    Schema::create('b_tales', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('a_tables_id')->unsigned();
     $table->foreign('a_tables_id')->references('id')->on('a_tables')->onDelete('cascade');
   }
}


来源:https://stackoverflow.com/questions/40863517/laravel-errno-150-foreign-key-constraint-is-incorrectly-formed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!