将新列添加到数据库 table,迁移失败并显示 - 没有要迁移的内容

Add new column to database table with migrations fails with - Nothing to migrate

我刚刚学会了如何使用迁移。我用这个模式成功创建了一个 table:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('units', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->timestamps();
    });
}

不幸的是我错过了专栏 path,所以我尝试添加它。首先,我在 laravel documentation 中通知自己学习如何添加新列。

来自文档:

The table method on the Schema facade may be used to update existing tables.

我的尝试:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('units', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->timestamps();
    });

    Schema::table('units', function (Blueprint $table) {
        $table->string('path');
    });
}

但是,在执行 php artisan migrate 之后,我得到 Nothing to migrate

我做错了什么?

您需要先回滚迁移,这将破坏包含所有数据的最后一个迁移表:

php artisan migrate:rollback

然后再次运行php artisan migrate命令。这是应用程序开发期间的最佳选择。

如果您出于某种原因不想这样做(应用已上线等),但只想添加一列,然后创建一个新的迁移并添加此代码:

public function up()
{
    Schema::table('units', function (Blueprint $table) {
        $table->string('path');
    });
}

和运行这些命令:

composer dumpauto
php artisan migrate

您必须为列更新创建一个新的迁移。或者,您可以回滚上次迁移,添加缺失的列,然后重新运行 迁移(假设您还没有运行 生产迁移)

回滚:php artisan migrate:rollback