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

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

/**
 * 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();
    });
}

不幸的是我错过了列路径,所以我试图添加它.首先,我通过laravel documentation告知myselfe,了解如何添加新列.

从文档:

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之后,我无法迁移.

我究竟做错了什么?

解决方法:

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

PHP artisan migrate:rollback

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

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

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

并运行以下命令:

composer dumpauto
PHP artisan migrate

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...