Laravel Doctrine\DBAL\DBALException Unknown column type enum requested

问题描述:

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

刚开始好像还有doctrine/dbal版本过高的问题(2+升到3,有时候大版本升级不兼容),后面安装的版本是2.1.7就剩下面这个报错了。
Doctrine\DBAL\Exception
Unknown column type “enum” requested.
在这里插入图片描述

大概意思就是不支持修改,网上折腾了一圈最后是执行原生sql的方法解决的。

更新脚本
    public function up()
    {
        Schema::table('script', function (Blueprint $table) {
            $table->enum('type', ['python','shell','python3'])->change();
        });
    }

解决方案:

执行原生sql
在这里插入图片描述
在这里插入图片描述

可以参考一下博客园 https://www.cnblogs.com/songlen/p/13305306.html
当用 Laravel 的数据迁移来更新 Enum 类型的字段时,比如:修改字段名

Schema::table('table_name', function (Blueprint $table) {
  $table->renameColumn('from', 'to');
});

当执行迁移命令的时候:

php artisan migrate
会报异常:

Doctrine\DBAL\DBALException:Unknown database type enum requested, 
Doctrine\DBAL\Platforms\MySQL57Platform may not support it.

当遇到这个问题的时候,首先确保已经安装了扩展 doctrine/dbal ,因为 Laravel 官方文档已经提到:

在修改字段之前,请确保将 doctrine/dbal 依赖添加到 composer.json 文件中。Doctrine DBAL 库用于确定字段的当前状态, 并创建对该字段进行指定调整所需的 SQL 查询

如果没有安装此扩展,请执行命令安装:

composer require doctrine/dbal

然后修改迁移文件

DB::getDoctrineSchemaManager()
    ->getDatabasePlatform()
    ->registerDoctrineTypeMapping('enum', 'string');

Schema::table('pattern_column_orders', function (Blueprint $table) {
    $table->renameColumn('payment', 'payment_method');
});

这时候再执行迁移命令发现已经可以成功执行了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值