laravel errno 150 外键约束格式不正确

2022-08-30 20:10:52

任何人都可以帮我解决这个问题吗?

有 3 个表,其中有 2 个外键:

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

运行迁移后出错:

[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

对于外键,引用的字段和引用字段必须具有完全相同的数据类型。

您可以同时创建字段,也可以将字段创建为有符号整数。但是,您将两个外键创建为无符号整数,因此创建这些键将失败。idusersfirms

您需要将子句添加到字段定义中,或者从外键字段中删除该子句。unsignedidunsigned


答案 2

这个答案并不比之前的六个答案更好,但它是一个更全面的答案,关于什么原因以及如何专门针对laravel进行修复。laravel-errno-150-foreign-key-constraint-is-incorrectly-formed

1)拼写:通常有时引用或引用的拼写错误可能会引发此错误,并且您不会知道,因为错误跟踪不是很具有描述性。column nametable name

2) 唯一 :引用的列必须是唯一的,或者通过在迁移中添加或添加或添加到列定义来编制索引。->primary()->unique()->index()

3)数据类型:引用和引用字段必须具有完全相同的数据类型。这一点怎么强调都不够。

  • 对于预期的数据类型为bigincrementsbigInteger('column_name')->unsigned();

  • 对于预期的是等。incrementsinteger('column_name')->unsigned();

4)剩余:当发生此错误时,并不意味着表未迁移而是迁移,而是未设置外键列,并且未将其添加到因此运行中将删除除故障表以外的其他表,因此建议手动删除故障表以避免进一步错误。migration tablephp artisan migrate:reset

5)顺序 :这通常是此错误的最常见原因,必须在其他工匠找不到集成外键的位置之前创建或迁移表。以确保迁移过程的订单重命名迁移文件示例:referencedreference table

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

这表明表A将始终在表B之前进行更改,我将重命名表B,现在它将在表A之前迁移。2014_10_11_100000_create_password_resets_table.php

6)启用外键:如果所有其他方法都失败,则在迁移代码示例之前添加: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迁移

提到我在评论中错过的任何其他修复,谢谢。


推荐