【问题标题】:Why Rollback raise Cannot drop index error?为什么回滚引发无法删除索引错误?
【发布时间】:2020-07-29 03:42:23
【问题描述】:

在我的 laravel 5.7 应用程序中,我需要将其他表中的表和引用添加到创建的表中。 我制作了 2 个迁移文件和 migrate 命令运行正常

但是运行回滚我有一个错误:

$ php artisan migrate
Migrating: 2020_03_08_162307_create_colors_table
Migrated:  2020_03_08_162307_create_colors_table
Migrating: 2020_03_08_162642_add_color_storage_spaces_table
Migrated:  2020_03_08_162642_add_color_storage_spaces_table
serge@athoe:/mnt/_work_sdb8/wwwroot/lar/BoxBooking2$ php artisan migrate:rollback
Rolling back: 2020_03_08_162642_add_color_storage_spaces_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1553 Cannot drop index 'storage_spaces_color_id_foreign': needed in a foreign key constraint (SQL: alter table `storage_spaces` drop index `storage_spaces_color_id_foreign`)

  at /mnt/_work_sdb8/wwwroot/lar/BoxBooking2/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1553 Cannot drop index 'storage_spaces_color_id_foreign': needed in a foreign key constraint")
      /mnt/_work_sdb8/wwwroot/lar/BoxBooking2/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:119

  2   PDOException::("SQLSTATE[HY000]: General error: 1553 Cannot drop index 'storage_spaces_color_id_foreign': needed in a foreign key constraint")
      /mnt/_work_sdb8/wwwroot/lar/BoxBooking2/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117

  Please use the argument -v to see more details.

文件是: 2020_03_08_162307_create_colors_table.php:

 <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateColorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('colors', function (Blueprint $table) {
            $table->smallIncrements('id');
            $table->string('color', 10)->unique();
            $table->string('title', 50)->unique();
            $table->enum('status', [ 'BU', 'UDL', 'UBPNCI', 'R'     , 'NPC'  , 'AUWN' , 'A'  ])->nullable()->comment(', BU=>Booked Units / Client Checked-In already, UDL=>Units Double Locked, UBPNCI - Unit Booked/Paid, not check-in, R - Reserved, AUWN - Available Units with notes (For repair, with water leakage, under maintenance & etch...), NPC - Non Paying Clients,    A=>Available');

            $table->timestamp( 'created_at')->useCurrent();
        });
        Artisan::call('db:seed', array('--class' => 'AdColorsWithInitData'));
    }

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

2020_03_08_162642_add_color_storage_spaces_table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddColorStorageSpacesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::table('storage_spaces', function (Blueprint $table) {
            $table->smallInteger(   'color_id'  )->unsigned()->nullable()->after("status");
            $table->foreign(   'color_id'  )->references('id')->on('colors')->onDelete('RESTRICT');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('storage_spaces', function (Blueprint $table) {
            $sm = Schema::getConnection()->getDoctrineSchemaManager();
            $indexesFound = $sm->listTableIndexes('storage_spaces');

            if(array_key_exists("storage_spaces_color_id_foreign", $indexesFound)) {
                $table->dropUnique("storage_spaces_color_id_foreign");
            }

            $table->dropColumn('color_id');
        });

    }
}

我尝试在 down 方法中删除 storage_spaces_color_id_foreign,但失败了。

哪种方式有效?

谢谢!

【问题讨论】:

    标签: laravel-5 migration


    【解决方案1】:

    你的错误是:

    无法删除索引“storage_spaces_color_id_foreign”.....

    索引名称应该在 dropForeign 函数中匹配。

    试试吧,希望它有效。

    Schema::table('storage_spaces', function (Blueprint $table) {
          $table->dropForeign('storage_spaces_color_id_foreign);
          $table->dropColumn('color_id');
     });
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 1970-01-01
      • 2019-10-26
      • 2013-05-03
      • 2014-12-11
      • 1970-01-01
      • 2021-03-21
      • 2015-09-02
      • 2012-01-16
      相关资源
      最近更新 更多