mysql - SQLSTATE[23000] : Integrity constraint violation: 1217

标签 mysql laravel database-migration laravel-artisan

在我的项目中创建多个迁移后,我想回滚以更新一些内容,但是当我尝试删除 projects 表时突然出现此错误。我仔细检查了外键约束,但找不到错误。

[Illuminate\Database\QueryException]                                         
  SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or upda  
  te a parent row: a foreign key constraint fails (SQL: drop table `projects`  
  )                                                                                                                                                           
  [PDOException]                                                               
  SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or upda  
  te a parent row: a foreign key constraint fails  

这是我的迁移: 1.创建表用户:

public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email', 50)->unique();
            $table->string('password', 60);
            $table->string('password_temp', 60);
            $table->string('code', 60);
            $table->boolean('active');
            $table->string('remember_token', 100);
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('users');
    }

2.创建客户表:

public function up()
    {
        Schema::create('clients', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index()->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('clients');
    }

3.创建项目表:

public function up()
    {
        Schema::create('projects', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->integer('status');
            $table->integer('client_id')->unsigned()->index()->nullable();
            $table->foreign('client_id')->references('id')->on('users')->onDelete('cascade');    
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('projects');
    }

在上次迁移中,我可能犯了什么错误?!迁移时我没有遇到任何问题,但它仅在我回滚以添加任何更改时出现。

知道为什么会这样吗?

最佳答案

在 down 函数中使用它。

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

关于mysql - SQLSTATE[23000] : Integrity constraint violation: 1217,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28064169/

相关文章:

java - 如何使用 Spring-data-JPA 使用 crudRepository 在 MySQL 数据库中附加多条记录?

php - Laravel 5. 具有多个条件的 FirstorNew

php - Laravel Lighthouse GraphQL - 在服务器端排序

php - MySQL 选择 3 行具有相同值的行

database - 当我想使用 db-migration 更新数据库时如何防止数据丢失?

mysql - 查询每周新用户数量 - 如何按年/周排序?

php - Codeigniter 事件记录语法错误

Laravel 4.2 迁移 - 在不删除列的情况下更改小数精度和比例

mysql - Django数据库迁移报错: duplicate key

php - 使用 PHP 将数据从 HTML 表单传递到 MySQL 数据库