Schema :: dropIfExists()在migration:rollback上删除数据库中的所有表

时间:2019-05-29 19:43:45

标签: php mysql laravel laravel-5 migration

我在命令行上运行了php artisan migrate:rollback,因此迁移删除了db中的所有表。

仅创建一个迁移,但在已使用的数据库中有其他表

laravel文档说:     “要删除现有表,可以使用drop或dropIfExists方法。”

,迁移代码上的功能down()是默认值,例如以下所示:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('image')->unsigned()->nullable();
            $table->string('name', 64);
            $table->string('slug', 64)->unique();
            $table->integer('price')->unsigned();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

在这种情况下,我认为Schema::dropIfExists('products')不能正常工作,这是一个错误吗?还是我(和Laravel文档)错误?

2 个答案:

答案 0 :(得分:0)

回滚,回滚最后一批的每次迁移。

如果您的所有迁移都是同一批次,则它将回滚所有迁移。因此,您的所有表将从数据库中删除。

enter image description here

答案 1 :(得分:0)

我知道这篇文章已有一年多的历史了,但这可能会对其他遇到此问题并正在寻找解决方案的人有所帮助。我猜这里发生了什么,如果您运行php artisan migrate:rollback,它将回滚最后一批的所有迁移。换句话说,它将在同一批中的所有迁移中运行所有down()函数。因此,如果您在还原更改(删除创建的表)的迁移中创建了down()函数,则这些迁移也将运行。

为防止这种情况,您可以添加--step参数来指示应回滚多少迁移,例如:php artisan migrate:rollback --step=1。然后,只有您的上一次迁移将被回滚。

相关问题