Laravel - errno: 150“迁移时外键约束形成不正确

时间:2021-05-05 08:09:09

标签: laravel

在我的 Laravel-8 应用程序中,我有这个迁移:

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id');
        $table->string('first_name',50);
        $table->string('last_name',50);
        $table->string('other_name',50)->nullable();
        $table->string('gender',20);
        $table->string('user_photo',350)->nullable();
        $table->integer('nationality_id');
        $table->string('marital_status',50);
        $table->date('dob')->nullable();
        $table->string('address', 300)->nullable();
        $table->integer('country_id')->nullable();
        $table->integer('state_id')->nullable();
        $table->integer('city_id')->nullable();
        $table->string('cv_file',350)->nullable();
        $table->text('summary')->nullable();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('nationality_id')->references('id')->on('countries');
        $table->foreign('country_id')->references('id')->on('countries');
        $table->foreign('state_id')->references('id')->on('state_origins');
        $table->foreign('city_id')->references('id')->on('cities');
    });
}

当我执行 php artisan migrate 时,出现此错误:

<块引用>

SQLSTATE[HY000]: General error: 1005 Can't create table hrm.profiles (errno: 150 "Foreign key constraint is wronglyformed") (SQL: alter table {{1} } 添加约束 profiles 外键 (profiles_user_id_foreign) 引用 user_id (users))

这是用户表:

id

我该如何解决这个问题?

谢谢

2 个答案:

答案 0 :(得分:0)

您需要在表中运行用作外键表的所有迁移。例如,您在 profile 表中将 user 定义为外来的,那么您必须在 profile 迁移文件之前运行 unsignedBigInteger() 迁移。对于所有其他人来说,这将是相同的过程。并确保使用 $table->unsignedBigInteger('user_id')

定义外键

ActiveWorkbook.SaveAs ("C:\Users\username\Desktop\" & "Fname " & Format(Now(), "DD.MM.YYYY" & ".xlsm")), _ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

答案 1 :(得分:0)

您的所有外键都需要未签名,将 bigInteger 更改为 unsignedBigInteger

主要外部 引用的类型必须相同。 bigIncrements() 需要 unsignedBigInteger()increments() 需要 unsignedInteger()