Laravel 5.5不能掉外国人

问题描述 投票:0回答:2

我的trophies表:

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();
    });
}

现在我想要掉外键season_id

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

当我运行php artisan migrate时,我收到下一个错误:

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`)

当我将项目从laravel 5.0升级到laravel 5.5时,可能会出现一些问题?

在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
laravel foreign-keys
2个回答
3
投票

试试这个......你需要放弃关系和专栏。

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

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

这有效。但为什么CONSTRAINT'trophies_season_id_foreign'被重命名为'trophies_ibfk_1'?

© www.soinside.com 2019 - 2024. All rights reserved.