如何在Laravel中修改迁移(migration)?

32

我想修改一个已存在的迁移。这是我的当前迁移类:

class CreateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::create('log_for_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('table_name');
            $table->string('error_message');
            $table->unsignedTinyInteger('error_code');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('log_for_user');
    }
}

我已经执行了php artisan migrate命令一次。现在我需要向error_message列添加->nullable()方法。因此,我编辑了我的迁移文件,就像这样:

.
.
    $table->string('error_message')->nullable();
.
.

但是当我再次执行 php artisan migrate 时,它说:

没有要迁移的内容。

我该如何应用新版本的迁移?

5个回答

35

您应使用以下命令创建新的迁移:

php artisan make:migration update_error_message_in_log_for_user_table

然后,在创建的迁移类中,使用change方法添加此行:

class UpdateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->change();
        });
    }
}

要进行这些更改并运行迁移,请使用以下命令:

php artisan migrate

要撤销更改,请使用以下命令:

php artisan migrate:rollback

您可以通过向回滚命令提供step选项来回滚有限数量的迁移。例如,以下命令将回滚最后五个迁移:

php artisan migrate:rollback --step=5

查看有关使用Migration修改列的更多信息,请访问此处


只有一个问题,什么时候应该使用change()方法?如果我想添加一个索引(比如unique()),而不是nullable(),我还需要像这样使用change()吗? $table->string('error_message')->unique()->change(); - stack
1
是的,change 方法告诉 Laravel 修改特定的查询! - Saumya Rastogi
1
根据Laravel的先决条件,安装以下包:composer require doctrine/dbal。@stack - Saumya Rastogi
我的最后一个问题是,我如何回退一步?我的意思是,假设我创建了一个新的迁移并添加了预期的更改,然后执行了 php artisan migrate,但现在我后悔了,我该如何回退一步? - stack

11

如果您的应用程序尚未投入生产,并且您正在填充数据,则最好的做法是运行:

php artisan migrate:refresh --seed

该命令将删除所有表并重新创建它们,然后种植数据。

如果您在开发过程中为每个更改创建其他迁移,最终会得到数百个迁移类。


1
在这种情况下,当前数据集将被删除,对吗? - stack
是的,数据将会丢失。但正确的方式是在开发过程中使用虚拟数据而非真实数据。 - Alexey Mezenin
我明白了。只有一个问题,--seed 到底是做什么的?我知道 migration:refresh 是干什么用的,但我不知道 --seed 到底是干什么的.. - stack
它运行种子,执行与 php artisan db:seed 命令相同的操作。您可以在这里了解更多信息。 - Alexey Mezenin
老实说,对我来说,“种子”数据库仍然很模糊。在数据库中,“种子”到底是什么意思?它是否通过虚假数据填充数据库?那这些数据从哪里来? - stack
通常情况下,您使用模型工厂生成的虚假数据。但是如果您想要,您也可以种植真实数据。 - Alexey Mezenin

4

还有一种选择。回滚迁移,编辑文件,然后再次运行它。

在开发新代码时,在本地开发服务器上调试错误时,这是一个相当普遍的做法。使用被接受答案中的方法,你可能最终需要创建17个迁移来创建单个表格!

php artisan migrate
# realize you've made an error
php artisan migrate:rollback
# edit your migration file
php artisan migrate

如果需要,可以在命令行上指定要向后退多少步。

# undo the last 3 migrations
php artisan migrate:rollback --step=3

或者,您可以指定需要撤消的特定迁移。

# undo one specific migration
php artisan migrate:rollback --path=./database/migrations/2014_10_12_100000_create_users_table.php

4
你可以使用 change 方法,它允许你将一些现有的列类型修改为新类型或修改列的属性。

例如将一个列修改为可为空:

Schema::table('log_for_user', function ($table) {
    $table->string('error_message')->nullable()->change();
});

首先,您需要使用doctrine/dbal

composer require doctrine/dbal

只有一个问题,什么时候应该使用 change() 方法?如果我想添加一个索引(例如 unique()),而不是 nullable(),我是否仍然需要像这样使用 change()$table->string('error_message')->unique()->change(); - stack
你不需要使用change方法来实现unique。只需在新的迁移文件中像这样使用 $table->unique('error_message'); - Amit Gupta
这是 "composer require doctrine/dbal"(你少了一个字母) - Luciano Fantuzzi

2

有两种方法可以实现此操作:

  1. 运行php artisan migrate:refresh。这将回滚所有迁移并迁移所有迁移。如果您运行此命令,则数据库中插入的所有数据都将丢失。
  2. 运行php artisan make:migration enter_your_migration_name_here。然后在您的迁移中插入以下内容:

    $table->string('error_message')->nullable()->change();

    然后运行php artisan migrate以使您的表更改生效。(请注意,当您执行此操作时,您需要在composer中要求composer require doctrine/dbal


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接