繁体   English   中英

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

[英]Cannot add foreign key constraint [Laravel 5.6]

TL;博士。 解:

多亏乔纳斯(Jonas)

问题是我所引用的表不是InnoDB。

我在变更迁移中添加了原始SQL语句,然后添加了外键:

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

原始问题

首先,在Stackoverflow警察让我破产之前,我知道这个问题可能占该网站数据库的83%。 但是我很特别(孩子,我知道我不是)。 但是我已经尝试了大多数常见的东西,但似乎没有任何效果。 所以可能我正在监督一些事情。

错误

常规错误:1215无法添加外键约束(SQL:alter table applications添加约束applications_user_id_foreign外键( user_id )在删除级联上引用usersid ))

这是我的迁移:

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);
    });
}
  1. 将迁移分为两个文件(一个创建和一个更改)。 甚至一个一个地添加每个引用。

4.-使用DB :: statement('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');
        ...
    });

UPDATE

对于尝试的放置方法:

  1. 对于创建迁移

    公共功能down(){Schema :: drop('applications'); }

    公共功能down(){Schema :: dropIfExists('applications'); }

2.-对于其他迁移

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

还没有运气。

引用的表还必须使用InnoDB引擎。

您可以使用原始SQL语句更改它们:

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

用于删除表:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM