向迁移失败的数据库表添加新列 - 无需迁移

Bla*_*ack 3 php database migration laravel laravel-5.4

我刚刚学会了如何使用迁移.我成功创建了一个包含此架构的表:

/**
 * 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();
    });
}
Run Code Online (Sandbox Code Playgroud)

不幸的是我错过了专栏path,所以我试图添加它.首先,我在laravel 文档中告知myselfe,以了解如何添加新列.

从文档:

Schema Facade上的表方法可用于更新现有表.

我的尝试:

/**
 * 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');
    });
}
Run Code Online (Sandbox Code Playgroud)

但是,执行后php artisan migrate我得到了Nothing to migrate.

我究竟做错了什么?

Ale*_*nin 6

您需要首先回滚迁移,这会破坏包含所有数据的上次迁移表:

php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)

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

如果你不想出于某种原因(应用程序是实时等),但只想添加一列,然后创建一个新的迁移并添加此代码:

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

并运行以下命令:

composer dumpauto
php artisan migrate
Run Code Online (Sandbox Code Playgroud)