无法添加外键约束[laravel 5.6]

nr9pn0ug  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(256)

热释光;医生解决方案:
多亏了乔纳斯。
问题是我所指的外来表不是innodb。
我在alter migrations中添加了原始sql语句,然后添加了外键: DB::statement("ALTER TABLE table ENGINE='InnoDB';"); 原始问题
首先,在警方逮捕我之前,我知道这个问题大概占了这个网站数据库的83%。但是我很特别(开玩笑的,我知道我不是)。但我试过大多数常用的东西,似乎都不管用。所以我可能在监督什么。
错误
常规错误:1215无法添加外键约束(sql:alter table applications 添加约束 applications_user_id_foreign 外键( user_id )参考文献 users ( id )删除时(级联)
这是我的迁移:

public function up()
{
    Schema::create("applications", function(Blueprint $table) {
        $table->engine = "InnoDB";
        $table->increments('id');
        $table->timestamps();
    });

    Schema::table('applications', function($table) {

        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->integer('job_request_id')->unsigned()->index();
        $table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
        $table->integer('status')->default(0);
    });
}

我已经试过了:
1

public function up()
{
    Schema::create("applications", function(Blueprint $table) {
        $table->increments('id');
        $table->timestamps();

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->integer('job_request_id')->unsigned();
        $table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
        $table->integer('status')->default(0);
    });
}

2

public function up()
{
    Schema::create("applications", function(Blueprint $table) {
        $table->engine = "InnoDB";
        $table->increments('id');
        $table->timestamps();

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->integer('job_request_id')->unsigned();
        $table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
        $table->integer('status')->default(0);
    });
}

将迁移拆分为两个文件(create和alter)。甚至一个接一个地添加每个引用。
4.-使用db::语句('set foreign_key_checks=0;');并且在迁移的开始和结束时为1。
5.-删除unsigned()和index()。
可能意味着什么:
1.-当我回滚迁移时,它不会删除表。所以,如果我回滚并迁移,会给我一个“已经存在的错误”。
2.-我已经有了引用相同项的迁移,即:

Schema::create('job_requests', function (Blueprint $table) {
        ...
        $table->integer('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users');
        ...
    });

更新
对于我尝试过的drop方法:
对于创建迁移
公共函数down(){schema::drop('applications');}
public function down(){schema::dropifexists('applications');}
2.-对于alter迁移

public function down()
{
    Schema::table('applications', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
        $table->dropColumn('user_id');
        $table->dropForeign(['job_request_id']);
        $table->dropColumn('job_request_id');
    });
}

更新2:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

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

public function up()
{
    Schema::create('job_requests', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('status')->default(0);
        $table->integer('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });
}

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

我又加了三个:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->engine = "InnoDB";
    });
}

///////////////////////////

public function up()
{
    Schema::table('job_requests', function (Blueprint $table) {
        $table->engine = "InnoDB";
    });
}

///////////////////////////

public function up()
{
    Schema::table('applications', function (Blueprint $table) {
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('applications', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
        $table->dropForeign(['job_request_id']);
    });
}

还不走运。

uemypmqf

uemypmqf1#

删除表格:

public function down()
{
  Schema::disableForeignKeyConstraints();
  Schema::dropIfExists('applications');
}
kx5bkwkv

kx5bkwkv2#

引用的表也必须使用 InnoDB 引擎。
您可以使用原始sql语句更改它们:

DB::statement("ALTER TABLE users ENGINE='InnoDB';");
DB::statement("ALTER TABLE job_requests ENGINE='InnoDB';");

相关问题