vendredi 4 décembre 2020

Unable to rename a foreign column in laravel 5.8 - column not found error

I am trying to rename a foreign column. So I write the following code:

Schema::table('floor_plans', function (Blueprint $table) {
    $table->dropForeign(['unit_type_id']);
    $table->renameColumn('unit_type_id ', 'utd_id');
    $table->foreign('utd_id')
        ->references('id')
        ->on('unit_type_details')
        ->onDelete('cascade');
});

But it throws

   Doctrine\DBAL\Schema\SchemaException  : There is no column with name 'unit_type_id ' on table 'floor_plans'.

  at D:\wamp64\www\amenity\vendor\doctrine\dbal\lib\Doctrine\DBAL\Schema\SchemaException.php:85
    81|      * @return \Doctrine\DBAL\Schema\SchemaException
    82|      */
    83|     public static function columnDoesNotExist($columnName, $table)
    84|     {
  > 85|         return new self(
    86|             sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
    87|             self::COLUMN_DOESNT_EXIST
    88|         );
    89|     }

Then I tried by turning foreign checks off:

DB::statement('SET FOREIGN_KEY_CHECKS=0');
$table->dropForeign(['unit_type_id']);
$table->renameColumn('unit_type_id ', 'utd_id');
DB::statement('SET FOREIGN_KEY_CHECKS=1');

But same error, the column not found.

Then I tried to drop foreign with exact ForeignKeyConstraint as :

DB::statement('SET FOREIGN_KEY_CHECKS=0');
$table->dropForeign('floor_plans_unit_type_id_foreign');
$table->renameColumn('unit_type_id ', 'utd_id');
DB::statement('SET FOREIGN_KEY_CHECKS=1');
$table->foreign('utd_id')
    ->references('id')
    ->on('unit_type_details')
    ->onDelete('cascade');  

Still the same error. I then even tried with Raw SQL as:

DB::statement("ALTER TABLE floor_plans
DROP FOREIGN KEY `floor_plans_unit_type_id_foreign`,
ADD CONSTRAINT `floor_plans_unit_type_detail_id_foreign` FOREIGN KEY (`unit_type_detail_id`) REFERENCES `unit_type_details` (`id`)");

And this gives me the following error:

SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'floor_plans_unit_type_id_foreign'; check that column/key exists (SQL: ALTER TABLE floor_plans
DROP FOREIGN KEY `floor_plans_unit_type_id_foreign`,
ADD CONSTRAINT `floor_plans_unit_type_detail_id_foreign` FOREIGN KEY (`unit_type_detail_id`) REFERENCES `unit_type_details` (`id`))

And here is the screenshot of the table. enter image description here

All I want is to rename unit_type_id to unit_type_detail_id.

#PS: the table is not empty.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire