Igor Ostapiuk
Igor Ostapiuk

Reputation: 619

Laravel 5.5 can't drop foreign

My trophies table:

public function up()
{
    Schema::create('trophies', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('tournament_id')->unsigned()->nullable();
        $table->foreign('tournament_id')
            ->references('id')
            ->on('tournaments')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;

        $table->integer('season_id')->unsigned()->nullable();
        $table->foreign('season_id')
            ->references('id')
            ->on('seasons')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;

        $table->integer('user_id')->nullable()->unsigned();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');
        ;

        $table->integer('team_id')->nullable()->unsigned();
        $table->foreign('team_id')
            ->references('id')
            ->on('teams')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;
        $table->timestamps();
    });
}

Now I want drop foreign key season_id

public function up()
{
    Schema::table('trophies', function (Blueprint $table) {
        $table->dropForeign(['season_id']);
    });
}

When I run php artisan migrate I receive next error:

In Connection.php line 664:

  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'trophies_season_id_foreign'; check that column/key exists (SQL: alter table `trophies` drop foreign key `trophies_season_id_foreign`)

Maybe some problem appeared when I have upgraded my project from laravel 5.0 to laravel 5.5?

In phpMyAdmin: enter image description here

    CREATE TABLE `trophies` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `tournament_id` int(10) unsigned DEFAULT NULL,
 `season_id` int(10) unsigned DEFAULT NULL,
 `user_id` int(10) unsigned DEFAULT NULL,
 `team_id` int(10) unsigned DEFAULT NULL,
 `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `tourney_id` int(10) unsigned NOT NULL,
 `tourney_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`id`),
 KEY `trophies_tournament_id_foreign` (`tournament_id`) USING BTREE,
 KEY `trophies_season_id_foreign` (`season_id`) USING BTREE,
 KEY `trophies_user_id_foreign` (`user_id`) USING BTREE,
 KEY `trophies_team_id_foreign` (`team_id`) USING BTREE,
 KEY `trophies_tourney_id_tourney_type_index` (`tourney_id`,`tourney_type`),
 CONSTRAINT `trophies_ibfk_1` FOREIGN KEY (`season_id`) REFERENCES `seasons` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `trophies_ibfk_2` FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `trophies_ibfk_3` FOREIGN KEY (`tournament_id`) REFERENCES `tournaments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `trophies_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=130 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Upvotes: 1

Views: 3615

Answers (2)

Igor Ostapiuk
Igor Ostapiuk

Reputation: 619

public function up()
{
    Schema::table('trophies', function (Blueprint $table) {
        $table->dropForeign('trophies_ibfk_1');
    });
}

This works. But why CONSTRAINT 'trophies_season_id_foreign' was rename to 'trophies_ibfk_1'?

Upvotes: 1

Gadurp1
Gadurp1

Reputation: 91

Try this out... You need to drop relationship and column.

$table->dropForeign(['season_id']);
$table->dropColumn('season_id');

Upvotes: 5

Related Questions