【问题标题】:Laravel Migration - Data type also changes when renaming columnLaravel 迁移 - 重命名列时数据类型也会改变
【发布时间】:2020-01-13 22:54:51
【问题描述】:

我想使用迁移重命名数据库中的列,但是当我尝试它时,数据类型也发生了变化。如何在不更改数据类型的情况下重命名列。

数据库中列的当前名称是 payment_amount_cash,其类型为double(10,2)

我试过了

$table->renameColumn('payment_amount_cash', 'payment_amount_full')->comment('Advanced payment')->default("0.00");

重命名列。然后在迁移之后,名称发生了变化,但数据类型也从 double(10,2) 更改为仅 double 从而使默认值从 更改0.000

我也尝试在重命名列的架构后更改数据类型。

$table->renameColumn('payment_amount_cash', 'payment_amount_full')->comment('Advanced payment')->default("0.00");
$table->double('payment_amount_full', 10,2)->change();

但是这个在迁移时给了我错误。

 Doctrine\DBAL\DBALException  : 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 list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). 
If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. 
Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). 
If the type name is empty you might have a problem with the cache or forgot some mapping information.

有了这个,我只想知道如何在不影响数据类型的情况下重命名列。迁移后,我想保留刚刚改名的列的数据类型。

【问题讨论】:

    标签: laravel-5.8 laravel-migrations


    【解决方案1】:

    我了解到 Schema 中存在关于枚举的问题,并且直到现在仍在讨论中。 我只是用另一种方式来更新列的数据类型。

    DB::statement('ALTER TABLE reservations MODIFY COLUMN payment_amount_full double(10,2)');
    

    这对我有用。如果还有其他方法可以做到这一点,请告诉我。谢谢。

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 2015-07-28
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2018-12-10
      • 2015-05-18
      • 2018-09-19
      • 2020-05-11
      相关资源
      最近更新 更多