jeudi 26 mars 2020

Why Rollback raise Cannot drop index error?

in my laravel 5.7 app I need to add table and reference in other table to created table. I made 2 migration files and migrate command runs ok

but running rollback I have an error :

$ 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.

The files are : 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');
        });

    }
}

I tried to remove storage_spaces_color_id_foreign in down method, but failed.

Which is the valid way ?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire