繁体   English   中英

如何更改数据类型并通过迁移添加列而不丢失数据? (laravel 5.3)

[英]How to change data type and add column with migration without losing data? (laravel 5.3)

我的迁移是这样的:

public function up()
{
    Schema::create('tests', function (Blueprint $table) {
        $table->increments('id_test');
        $table->string('name', 50);
        $table->timestamps();
        $table->softDeletes();
    });
}

public function down()
{
    Schema::drop('tests');
}

我想更改数据类型并在表测试中添加列。 然后更新数据库中的表。 我这样编辑:

public function up()
{
    Schema::create('tests', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_test', 18);
        $table->string('name', 50);
        $table->timestamps();
        $table->softDeletes();
    });
}

public function down()
{
    Schema::drop('tests');
}

然后我运行: php artisan migrate:refresh

我丢失了数据库中的数据

有没有人可以帮助我?

使用以下命令创建一个新的迁移文件:

php artisan make:migration name_of_your_file

它的功能应该是这样的

    public function up()
        {
            Schema::table('tests', function($table) {
                  $table->renameColumn('id_test', 'id');
                  $table->string('id_test', 18);
            });
        }


   public function down()
        {
            Schema::table('tests', function($table) {
                 $table->dropColumn('id_test');
            });
        }

现在只需在命令下运行

php artisan migrate

注意:不要使用refresh它会删除数据

要了解有关Laravel迁移的更多信息,请参阅: https ://laravel.com/docs/5.3/migrations#modifying-columns

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM