1
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table `users`)

我知道这已经被回答了好几次,但我仍然无法弄清楚我的迁移有什么问题。

首先我打电话给合作伙伴迁移

public function up()
{
    Schema::create('partners', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('admin_id')->unsigned();
        $table->string('company_name', 50);
        ...
}
public function down()
{
    Schema::drop('partners');
}

然后我调用用户迁移

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('partner_id')->unsigned();
        $table->string('email', 70)->unique();
        $table->string('first_name', 50);
        $table->string('last_name', 50);
        $table->string('password', 60);
        $table->string('image', 200)->nullable();
        $table->string('gender', 10)->nullable();
        $table->string('phone', 25)->nullable();
        $table->string('nationality', 50)->nullable();
        $table->string('address', 200)->nullable();
        $table->boolean('is_active')->default(0);
        $table->string('single_signon', 30)->nullable();

        // Checks if mysql or mariadb supports json data type
        if ((DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')) {
            $table->json('settings')->nullable();
        } else {
            $table->text('settings')->nullable();
        }

        $table->softDeletes();
        $table->rememberToken();
        $table->timestamps();
        $table->foreign('partner_id')->references('id')->on('partners')->onDelete('cascade');
    });
}

public function down()
{
    Schema::table('users', function(Blueprint $table) {
        $table->dropForeign('users_partner_id_foreign');
    });

    Schema::drop('users');
}

最后我调用通知迁移:

public function up()
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('notification_type', 10);

            // Checks if mysql or mariadb supports json data type
            if ((DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')) {
                $table->json('notification')->nullable();
            } else {
                $table->text('notification')->nullable();
            }
            $table->boolean('seen');
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
public function down()
{
    Schema::table('notifications', function(Blueprint $table) {
        $table->dropForeign('notifications_user_id_foreign');
    });
    Schema::drop('notifications');
}

任何人都可以弄清楚这有什么问题吗?我正在使用 laravel 5.3 和 php7

4

2 回答 2

0
public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');
    Schema::drop('tableName');
    DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}

删除表时禁用外键检查

于 2018-06-03T18:50:02.590 回答
0

问题在于迁移顺序。最好将迁移包含在一个文件中并将子级放在父级之前,如下所示:

public function down(){
  Schema::drop('notifications');
  Schema::drop('users');
  Schema::drop('partners');
}

这是因为 Laravel 试图在users表之前删除表notifications,其中包括在表user_id中具有引用的users列。

于 2016-09-30T16:21:38.383 回答