【问题标题】:"Migrate reset" not working when there is an error in code代码出错时“迁移重置”不起作用
【发布时间】:2019-10-08 14:05:34
【问题描述】:

在基本的 Laravel 应用程序中,我在 User 类中添加了:

使用软删除;

应用程序不再工作,因为 MySql 数据库的 User 表中没有 'deleted_at' 列。我正在尝试通过删除所有表并重新创建它们(或删除所有表)来添加此列:

工匠迁移:刷新 --seed

工匠迁移:重置

工匠迁移:新鲜

在所有情况下我都会收到错误:

Illuminate\Database\QueryException : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.deleted_at' in 'where clause' (SQL: select exists(select * from users where email = user@ gmail.com 和users.deleted_at 为空)为exists

我不明白。如果我要恢复迁移,工匠应该只在我的 User 类上调用 down() 方法,即:

public function down()
{
    Schema::dropIfExists('users');
}

如果未找到列,删除表不应该失败。

附:我正在使用 docker-compose 来运行 php、mysql、artisan、composer、nginx 和 nodejs——如果这很重要的话。

【问题讨论】:

  • 你试过php artisan migrate:fresh吗?

标签: mysql laravel database-migration laravel-5.8


【解决方案1】:

最初给出的答案是不正确的,因为问题也有些不正确,在聊天中讨论后,我们发现作者也在 AppServiceProvider 中进行了迁移,这阻止了通过 CLI 进行实际迁移。

您可以阅读the chat here。从 AppServiceProvider 中删除工匠调用解决了这个问题

【讨论】:

  • 所有迁移命令都存在相同的错误,包括 migrate:fresh
  • @Uros 刷新命令的行为不同,你试过吗?
  • @Uros 我无法想象为什么migrate:fresh 不起作用,因为它强制删除所有表并重新开始。 migrate:refresh 但是会回滚会引发此错误的迁移。在这种情况下,这个错误似乎与您的迁移无关,它是其他地方的代码。
  • 当我注释掉这行代码('use SoftDeletes;')时,迁移运行没有问题。我同意你的观点,这应该是一个错误。也许问题在于在 docker 中运行它
  • @Uros 您是否有任何查询在您的服务提供商中运行?
【解决方案2】:

您是否也在迁移中添加了软删除?

public function up()
{
    Schema::table('users', function(Blueprint $table)
    {
        $table->softDeletes();
        ...
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2021-08-14
    • 2015-07-24
    • 2018-11-30
    相关资源
    最近更新 更多