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

Updating Column Attributes in Laravel 6 Migrations Triggers Doctrine\DBAL\Exception: Unknown Column Type “tinyinteger” Requested

执行迁移时报错,Doctrine\DBAL\Exception : Unknown column type "tinyinteger" requested.

1. In Laravel 6, update the column attributes.

PHP

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. An error occurs when running the migration: Doctrine\DBAL\Exception : Unknown column type “tinyinteger” requested. See Figure 1.

Error when running the migration: Doctrine\DBAL\Exception : Unknown column type "tinyinteger" requested.
Figure 1
Plaintext


   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. Since the data in this column can be deleted, I decided to drop the column and then add it again.

PHP

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. Error: Illuminate\Database\QueryException : SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ‘step’. See Figure 2.

Error: Illuminate\Database\QueryException : SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'step'.
Figure 2
Plaintext
   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. Finally, I had to split this into two migration files: first drop the column, and then add the column. The migration ran successfully.

PHP

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

PHP

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. If the column data needs to be preserved, you can refer to https://www.shuijingwanwq.com/2022/06/02/6483/ and use DB::statement.

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

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

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

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

关于我  ·  GitHub  ·  邮件联系