首页 > 解决方案 > 迁移 - 无法更改双精度数据类型值

问题描述

我有一个使用此迁移代码创建的现有表:

Schema::create('mytable', function (Blueprint $table) {

 $table->increments('id');
 $table->double('mycolumn',8,2)->unsigned()->default(0);
 $table->timestamps();

});

然后我创建了另一个迁移文件,mycolumn用下面的迁移文件调整我的字段的值范围。

Schema::table('mytable', function (Blueprint $table) {

 $table->double('mycolumn',15,2)->unsigned()->default(0)->change();

});

但是我收到一个错误:

In DBALException.php line 283:

  Unknown column type "double" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a li
  st of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgott
  en to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getM
  appedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

我错过了什么?

标签: laravel

解决方案


您在文档中缺少此内容

只有以下列类型可以“更改”:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger 和 unsignedSmallInteger

所以double不能改变。

我没有尝试过,但您可以使用这样的 RAW MySQL 查询,当然首先在本地尝试:

DB::statement('alter table mytable modify mycolumn DOUBLE(15,2) DEFAULT 0');

推荐阅读