【问题标题】:Is it possible to change the datatype of a column from int to double without losing data with Laravel migration是否可以通过 Laravel 迁移将列的数据类型从 int 更改为 double 而不会丢失数据
【发布时间】:2020-06-16 22:30:25
【问题描述】:

我正在尝试更改我的专栏的数据类型之一。我创建表的迁移是:

public function up()
    {
        Schema::create('place_ratings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('places_id');
            $table->foreign('places_id')->references('id')->on('places');
            $table->integer('rating');
            $table->mediumText('comments');
            $table->timestamps();
        });
    }

我想将评级表的数据类型从integer 更改为float。我的表中已经有一些数据,我不想丢失它们。那么,我该怎么做呢?

【问题讨论】:

  • 最简单的解决方案是导出数据并在本地数据库中检查。

标签: php mysql laravel alter-table laravel-migrations


【解决方案1】:

您应该能够使用简单的迁移来更改该列,请参阅https://laravel.com/docs/5.8/migrations#modifying-columns 了解更多信息:

您需要先安装composer require doctrine/dbal 才能生成更改这些列的sql。

Schema::table('place_ratings', function (Blueprint $table) {
    $table->decimal('rating', 16, 4)->change();
});

16 和4 表示总长度和小数精度,因此在这种情况下,它的范围为0.0000(点之前没有,之后是4)到000000000000.0000(点之前的12)点,后面 4 个,所以总共 16 个)。

请务必先在本地运行它,以确保您没有不需要的副作用。

【讨论】:

  • 我在该列中有一些数据。并且评级列的类型现在是 int。当我进行迁移并将数据类型更改为浮动时,是否有可能丢失评级列中的这些数据?
  • 更新:工作。谢谢
猜你喜欢
  • 2017-03-03
  • 2020-03-07
  • 2012-06-14
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2014-10-16
相关资源
最近更新 更多