php - 将新列添加到数据库表中,迁移失败并显示 - 无需迁移

标签 php database laravel migration 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();
    });
}

不幸的是,我错过了 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 - 将新列添加到数据库表中,迁移失败并显示 - 无需迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42485951/

相关文章:

php - 删除 Laravel 中的缓存文件

php - MySQL:无法使用 WHERE 子句返回 POINT 类型

sql - 选择更新等待网络操作?

php - 如何检查用户是否通过 Laravel 上的 cookie session 进行身份验证?

laravel - 工作箱:构建项目时无法缓存所有文件

php - 安装命令在 Composer 中不起作用

javascript - 在刷新浏览器之前,localhost 中的应用程序的结果是挂起的

MySql 从另一个连接和分组依据更新表

python - 在数据库中存储逻辑

laravel - withTrashed 上的 hasManyThrough 关系