微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐