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

在 Laravel 6 的数据库迁移中,更新字段属性时,Doctrine\DBAL\Exception : Unknown column type “tinyinteger” requested.

执行迁移时报错,Doctrine\DBAL\Exception : Unknown column type "tinyinteger" requested.
1、在 Laravel 6 中,更新字段属性


Schema::table('theme_installation_task', function (Blueprint $table) {
	$table->unsignedTinyInteger('step')->default(0)->comment('步骤,1:排队中;2:下载 ZIP;3:解压缩 ZIP;4:执行安装命令;5:复制主题文件;6:同步主题的素材至CDN')->change();
});


2、执行迁移时报错,Doctrine\DBAL\Exception : Unknown column type “tinyinteger” requested.。如图1
执行迁移时报错,Doctrine\DBAL\Exception  : Unknown column type "tinyinteger" requested.
图1



   Doctrine\DBAL\Exception  : Unknown column type "tinyinteger" 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("tinyinteger")
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\TypeRegistry.php:37

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

  Please use the argument -v to see more details.


3、由于此字段的数据允许删除,所以决定删除此字段后,再添加。


Schema::table('theme_installation_task', function (Blueprint $table) {
	$table->dropColumn('step');
	$table->unsignedTinyInteger('step')->default(0)->after('processing_failed')->comment('步骤,1:排队中;2:下载 ZIP;3:解压缩 ZIP;4:执行安装命令;5:复制主题文件;6:同步主题的素材至CDN');
});


4、报错:Illuminate\Database\QueryException : SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ‘step’ 。如图2
报错:Illuminate\Database\QueryException  : SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'step' 。
图2


   Illuminate\Database\QueryException  : SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'step' (SQL: alter table `theme_installation_task` add `step` tinyint unsigned not null default '0' comment '步骤,1:排队中;2:下载 ZIP;3:解压缩 ZIP;4:执行安装命令;5:复制主题文件;6:同步主题的素材至CDN' after `processing_failed`)

  at E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'step'")
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exception.php:18

  2   Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:119

  Please use the argument -v to see more details.


5、最后不得不拆分为 2 个迁移文件,先删除字段,然后再添加字段。执行迁移成功。


Schema::table('theme_installation_task', function (Blueprint $table) {
	$table->dropColumn('step');
});




Schema::table('theme_installation_task', function (Blueprint $table) {
	$table->unsignedTinyInteger('step')->default(0)->after('processing_failed')->comment('步骤,1:排队中;2:下载 ZIP;3:解压缩 ZIP;4:执行安装命令;5:复制主题文件;6:同步主题的素材至CDN');
});


6、如果字段数据需要保留,则可以参考:https://www.shuijingwanwq.com/2022/06/02/6483/ ,使用 DB::statement。

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理