【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1217SQLSTATE [23000]:违反完整性约束:1217
【发布时间】:2015-03-19 19:01:10
【问题描述】:

在我的项目中创建多个迁移后,我想回滚以更新一些内容,但是当我尝试删除 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');
    }

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

知道为什么会这样吗?

【问题讨论】:

    标签: mysql laravel database-migration laravel-artisan


    【解决方案1】:

    在 down 函数中使用这个。

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

    【讨论】:

      【解决方案2】:

      如果上面提到的只有 3 个表,你的迁移没有问题,因为我自己尝试过

      php artisan migrate
      

      创建表格和

      php artisan migrate:rollback
      

      回滚。

      我知道的一件事是 Laravel 会根据迁移文件的时间戳来假设迁移的顺序。

      因此,我很确定还有另一个表具有对 projects 表的外键引用,但错误消息是(SQL:drop table projects

      尝试使用php artisan tinker,然后使用Schema::drop('some_table_name');,其中some_table_name 是引用projects 表的表,然后再次删除projects 表。

      【讨论】:

      • 这不是问题的答案。
      【解决方案3】:

      当您在表中使用外键时,您需要在删除表之前使用 dropForeign 方法删除这些外键,否则您将遇到当前遇到的完整性约束问题。

      public function down()
      {
          Schema::table('projects', function(Blueprint $table) {
              $table->dropForeign('projects_client_id_foreign');
          });
      
          Schema::drop('projects');
      }
      

      【讨论】:

        【解决方案4】:

        此问题通常是由编辑/添加到现有迁移造成的。 在处理外键时创建新的迁移文件,或者准备好可能转储/删除整个数据库并刷新。

        【讨论】:

          猜你喜欢
          • 2014-08-18
          • 2015-11-27
          • 2017-09-11
          • 2015-01-20
          • 2015-07-17
          • 2021-03-08
          • 2021-07-23
          • 1970-01-01
          相关资源
          最近更新 更多