没有不值得去解决的问题,也没有不值得去学习的技术!

How to Modify Column Types in Laravel 6 Database Migrations

报错:Doctrine\DBAL\Exception : Unknown column type "uuid" requested.

1. Currently, the theme_id field in the theme_asset table has a type of varchar(255). The previous migration code is as follows. See Figure 1.

Currently, the theme_id field in the theme_asset table has a type of varchar(255). The previous migration code is as follows
Figure 1
PHP

$table->string('theme_id')->nullable(false)->comment('主题Id');

2. Now I plan to add a new migration file to change the type of the theme_id field to: uuid, which is char(36).

PHP

$table->uuid('theme_id')->change();

3. Error: Doctrine\DBAL\Exception : Unknown column type “uuid” requested. See Figure 2.

Error: Doctrine\DBAL\Exception : Unknown column type "uuid" requested.
Figure 2
Plaintext

PS E:\wwwroot\object> php artisan module:migrate ThemeStoreDB
Migrating: 2022_05_20_160818_update_theme_asset_table

   Doctrine\DBAL\Exception  : Unknown column type "uuid" 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.

  at E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php:282
    278|      * @return Exception
    279|      */
    280|     public static function unknownColumnType($name)
    281|     {
  > 282|         return new Exception('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
    283|             'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
    284|             'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
    285|             'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
    286|             'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .

  Exception trace:

  1   Doctrine\DBAL\DBALException::unknownColumnType("uuid")
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\TypeRegistry.php:37

  2   Doctrine\DBAL\Types\TypeRegistry::get("uuid")
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\Type.php:237

  Please use the argument -v to see more details.

4. After changing it to char, the error still occurs: Unknown column type “char” requested.

PHP

$table->char('theme_id', 36)->change();

5. I finally decided to use DB::statement. The code is as follows:

PHP

DB::statement('ALTER TABLE ' . DB::connection()->getTablePrefix() . 'theme_asset MODIFY theme_id char(36) NOT NULL COMMENT \'文件内容\'');

6. I checked and compared the SQL for generating the table, and it meets expectations. See Figure 3.

Checked and compared the SQL for generating the table, and it meets expectations
Figure 3
SQL

`theme_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主题Id',

`theme_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主题Id',

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系