【问题标题】:How can I modify a migration in Laravel?如何在 Laravel 中修改迁移?
【发布时间】:2017-05-03 12:23: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 命令。现在我需要将->nullable() 方法添加到error_message 列。所以我编辑了我的迁移,如下所示:

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

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

没有要迁移的东西。

如何应用新版本的迁移?

【问题讨论】:

    标签: php laravel migration


    【解决方案1】:

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

    当您解决新代码中的错误时,这在本地开发服务器上是相当常见的事情。使用已接受答案中的方法,您最终可能需要 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
    

    【讨论】:

      【解决方案2】:

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

      例如修改一列可以为空:

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

      但首先你需要doctrine/dbal package

      composer require doctrine/dbal
      

      【讨论】:

      • 只有一个问题,我应该什么时候使用change() 方法?如果我想添加一个索引(如unique())而不是nullable(),我还需要像这样使用change()吗? $table->string('error_message')->unique()->change();
      • unique 不需要 change 方法。只需在新的迁移文件中像这样$table->unique('error_message'); 使用它。
      • 它是“作曲家需要学说/dbal”(你少了一个字母)
      【解决方案3】:

      你应该使用命令创建一个新的迁移:

      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
      

      查看更多关于Modifying columns with Migration

      【讨论】:

      • 只有一个问题,我应该什么时候使用change() 方法?如果我想添加一个索引(如unique())而不是nullable(),我还需要像这样使用change()吗? $table->string('error_message')->unique()->change();
      • 是的,change 方法告诉 laravel 修改那个特定的查询!
      • @stack - 根据 laravel perquisite 安装这个包 - composer require doctrine/dbal
      • 最后一个问题,我怎样才能退后一步?我的意思是,假设我已经创建了一个新的迁移并添加了预期的更改并执行了php artisan migrate,我现在后悔了,我该如何退一步?
      【解决方案4】:

      有两种方法可以做到这一点:

      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 require doctrine/dbal)

      【讨论】:

        【解决方案5】:

        如果您的应用未投入生产并且您为数据播种,那么您能做的最好的事情就是运行:

        php artisan migrate:refresh --seed
        

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

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

        【讨论】:

        • 在这种情况下,当前数据集将被删除,对吧?
        • 是的,数据会丢失。但正确的做法是在开发过程中播种数据,而不是使用真实数据。
        • 我明白了。只是一件事,--seed 到底做了什么?我知道migration:refresh 做什么,但我不知道--seed 做什么..
        • 它运行播种机,与php artisan db:seed 命令相同。你可以阅读它here。
        • 老实说,seed 数据库对我来说还是模糊的.. "seed" 在数据库中到底是什么意思?它是否用虚幻数据填充数据库?那么这些数据是从哪里来的呢?
        猜你喜欢
        • 2021-01-31
        • 2017-10-03
        • 2019-05-27
        • 2019-02-18
        • 2021-03-02
        • 2020-02-02
        • 2020-11-07
        • 1970-01-01
        相关资源
        最近更新 更多