【问题标题】:Laravel 6 on Migration: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraintLaravel 6 on Migration: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
【发布时间】:2021-01-09 11:58:45
【问题描述】:

我正在尝试迁移我的表,但是当我进行 laravel 迁移时,会显示一个错误。

SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter table subscribers 添加约束 subscribers_user_id_foreign 外键 (user_id) 引用 users (id) 在删除集 null )

这是我要迁移的表:

Schema::create('subscribers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('account_no');
            $table->enum('type',['individual','company'])->default('individual');
            $table->string('title')->nullable();
            $table->string('firstname');
            $table->string('middlename');
            $table->string('lastname');
            $table->string('suffix')->nullable();
            $table->enum('gender',['male','female']);
            $table->date('birthday')->nullable();
            $table->string('contact_no');
            $table->string('email');
            $table->tinyInteger('status')->default(1);
            $table->integer('user_id')->unsigned();
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');
        });

【问题讨论】:

标签: mysql laravel


【解决方案1】:

立即尝试

改变这个

$table->unsignedBigInteger('user_id');
Schema::create('subscribers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('account_no');
            $table->enum('type',['individual','company'])->default('individual');
            $table->string('title')->nullable();
            $table->string('firstname');
            $table->string('middlename');
            $table->string('lastname');
            $table->string('suffix')->nullable();
            $table->enum('gender',['male','female']);
            $table->date('birthday')->nullable();
            $table->string('contact_no');
            $table->string('email');
            $table->tinyInteger('status')->default(1);
            $table->unsignedBigInteger('user_id');
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2021-04-24
    • 2021-10-15
    • 1970-01-01
    • 2012-10-10
    • 2020-01-14
    相关资源
    最近更新 更多