Почему откат не может сбросить ошибку индекса? - PullRequest
0 голосов
/ 26 марта 2020

в моем приложении laravel 5.7 Мне нужно добавить таблицу и ссылку в другой таблице в созданную таблицу. Я сделал 2 файла миграции, и команда переноса работает нормально

, но при выполнении отката у меня возникает ошибка:

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

    }
}

Я пытался удалить storage_spaces_color_id_foreign в методе down, но не смог.

Какой правильный способ?

Спасибо!

...