Laravel 5.5不能掉外国人

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel 5.5不能掉外国人相关的知识,希望对你有一定的参考价值。

我的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
答案

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

$table->dropForeign(['season_id']);
$table->dropColumn('season_id');
另一答案
public function up()
{
    Schema::table('trophies', function (Blueprint $table) {
        $table->dropForeign('trophies_ibfk_1');
    });
}

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

以上是关于Laravel 5.5不能掉外国人的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.5 中的迁移问题

laravel5.5学习2-路由系统

检查 Laravel 5.5 中是不是存在记录 [关闭]

Laravel 5.5:如何定义可在所有控制器中使用的全局变量?

震惊,这篇文章竟然因为色情原因被删除 | laravel 5.5 相对 5.1 中的变动

laravel 5.5 数据返回数组