Laravel 迁移-无法更改双数据类型值
我已使用此迁移代码创建了一个现有表:Laravel 迁移-无法更改双数据类型值,laravel,Laravel,我已使用此迁移代码创建了一个现有表: Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->double('mycolumn',8,2)->unsigned()->default(0); $table->timestamps(); }); 然后,我创建了另一个迁移文件,用下面的迁移文件调整mycolumn字段的值范围 Schema
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.
我错过了什么?你错过了 只能更改以下列类型:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger和unsignedSmallInteger 所以不能改变 我还没有尝试过,但您可以像这样使用原始MySQL查询,首先在本地尝试,当然:
DB::statement('alter table mytable modify mycolumn DOUBLE(15,2) DEFAULT 0');
你在地图上找不到这个 只能更改以下列类型:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger和unsignedSmallInteger 所以不能改变 我还没有尝试过,但您可以像这样使用原始MySQL查询,首先在本地尝试,当然:
DB::statement('alter table mytable modify mycolumn DOUBLE(15,2) DEFAULT 0');
我不确定这是否有帮助,但在这种情况下,您可以使用十进制或浮点,而不是双精度,尽管如果您需要有限的小数位数,浮点将不起作用。 所以它看起来像:
$table->decimal('mycolumn',15,2)->unsigned()->default(0)->change();
我不确定这是否有帮助,但在这种情况下,您可以使用十进制或浮点,而不是双精度,尽管如果您需要有限的小数位数,浮点将不起作用。 所以它看起来像:
$table->decimal('mycolumn',15,2)->unsigned()->default(0)->change();
不能像对其他类型那样更改double,可以使用doctor\DBAL\Type修复它
您可以通过以下方式进行修复:
use Doctrine\DBAL\Types\FloatType;
use Doctrine\DBAL\Types\Type;
public function up() {
if (!Type::hasType('double')) {
Type::addType('double', FloatType::class);
}
Schema::table('mytable', function($table) {
$table->double('mycolumn',15,2)->default(0)->change();
.....
});
}
不能像对其他类型那样更改double,可以使用doctor\DBAL\Type修复它
您可以通过以下方式进行修复:
use Doctrine\DBAL\Types\FloatType;
use Doctrine\DBAL\Types\Type;
public function up() {
if (!Type::hasType('double')) {
Type::addType('double', FloatType::class);
}
Schema::table('mytable', function($table) {
$table->double('mycolumn',15,2)->default(0)->change();
.....
});
}
哦,明白了,有什么建议吗?如果你没有任何数据,我会删除该列并创建一个新列。我只是删除了该列,它可以根据需要工作,谢谢。接受并投票!我更新了我的答案以删除该列,以防其他人需要它。在down方法中应该处理什么?哦,明白了,有什么建议吗?如果你没有任何数据,我会删除该列并创建一个新列。我只是删除了该列,它按需工作,谢谢。接受并投票!我更新了我的答案,以便在其他人需要时删除该集。在down方法中应该处理什么?您使用的是哪个Laravel版本Laravel版本:5.7您使用的是哪个Laravel版本Laravel版本:5.7