Invalid foreign key constraint laravel errno 150

Can someone help me solve this problem?

There are 3 tables with two 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 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")
+10
source share
8 answers

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

You create fields idin usersand firmsas signed integers. However, you create both foreign keys as unsigned integers , so the keys are not created.

unsigned id, unsigned .

+21

- .

, .

:

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

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

, bigInteger, . .

, unsigned(), nullable() .. , .

+2

, laravel-errno-150-foreign-key-constraint-is-incorrectly-formed , , laravel-errno-150-foreign-key-constraint-is-incorrectly-formed laravel.

1) : column name table name , , , .

2) : ->primary() ->unique() .

3) : . .

  • bigincrements : bigInteger('column_name')->unsigned();

  • increments - integer('column_name')->unsigned(); ..

4) : , , , , migration table , php artisan migrate:reset , , .

5) : : , referenced , reference table , . , :

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

, A 2014_10_11_100000_create_password_resets_table.php B, , B 2014_10_11_100000_create_password_resets_table.php A.

6) Schema::enableForeignKeyConstraints(); : Schema::enableForeignKeyConstraints(); Schema::enableForeignKeyConstraints(); function up() :

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

, laravel laravel.

, , .

+2

, laravel , , . "- > unsigned()".

+1

:

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

:

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

, laravel MySQL, .

. .

The way I solved this problem was to simply drop the entire database and recreate it. Then migrate again.

0
source

if you set the reference fields and you had exactly the same data type, but the error exists, you can change the date migration file only for it to work

0
source

For PHP laravel 5.8 use an unsigned modifier in this format

$table->unsignedBigInteger('user_id');

delete all tables in the database and start the migration again

0
source

Source: https://habr.com/ru/post/1662256/


All Articles