Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Unknown column type "mediumInteger" requested" error after downgrading doctrine/dbal to 2.* #36509

Closed
sentisso opened this issue Mar 8, 2021 · 7 comments

Comments

@sentisso
Copy link

sentisso commented Mar 8, 2021

  • Laravel Version: 8.0
  • PHP Version: 7.4.3
  • Database Driver & Version: mysql Ver 15.1 Distrib 10.4.11-MariaDB

Description:

I was having an issue with migrations when trying to change column types. It was the Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found error after trying to migrate. I managed to solve this specific error by downgrading doctrine/dbal to 2.*.

But now I get this error when trying to migrate to change column types:

C:\path_to_my_project>php artisan migrate

   Doctrine\DBAL\Exception 

  Unknown column type "mediuminteger" 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 d
atabase introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#regist
erDoctrineTypeMapping() 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 C:\path_to_my_project\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 ' .

  1   C:\path_to_my_project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\TypeRegistry.php:39
      Doctrine\DBAL\DBALException::unknownColumnType("mediuminteger")

  2   C:\path_to_my_project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\Type.php:233
      Doctrine\DBAL\Types\TypeRegistry::get("mediuminteger")

This is the migration code:

public function up()
{
    Schema::table('opening_hours', function (Blueprint $table) {
        $table->mediumInteger("open_time")->unsigned()->nullable()->change();
        $table->mediumInteger("close_time")->unsigned()->nullable()->change();
    });
}

Steps To Reproduce:

theoretically:

  1. Create a migration file for modifying an existing table
  2. Try to change a columns type value to mediumInteger
  3. php artisan migrate

There might be the Driver not found error, so follow to this issue and downgrade doctrine/dbal to 2.*

@sentisso sentisso changed the title "Unknown column type "mediumInteger" requested" after downgrading doctrine/dbal to 2.* "Unknown column type "mediumInteger" requested" error after downgrading doctrine/dbal to 2.* Mar 8, 2021
@driesvints
Copy link
Member

What exact DBAL version are you running?

@sentisso
Copy link
Author

sentisso commented Mar 8, 2021

@driesvints composer require doctrine/dbal:2.* installed 2.12.1

@driesvints
Copy link
Member

And which Laravel version? (please fill out the issue template correctly next time)

@sentisso
Copy link
Author

sentisso commented Mar 8, 2021

@driesvints Sorry, now I noticed I actually provided the composer requirement itself.

Installed laravel/framework package is v8.14.0

@driesvints
Copy link
Member

Please update to the latest version of the framework that supports this DBAL version. Thanks

@rozsazoltan
Copy link

rozsazoltan commented Feb 7, 2024

I know it's an old issue, but I ran into it as well. I didn't want to resort to workarounds, so I found the fix for the problem.

This way, there's no need to touch any packages. You can simply declare the MediumInteger type in your application and use it in the Doctrine DBAL package.


Requirements

  • Laravel 8.x or higher
  • Doctrine DBAL 3.x or higher

Solution

Create the class corresponding to our type based on the following template:

App\Database\Types\MediumInteger.php
<?php

namespace App\Database\Types;

use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * Custom type for medium integer.
 */
class MediumInteger extends IntegerType
{
    // Important: leave it as is, Laravel Schema will look for it by this name
    const NAME = 'mediuminteger';

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        // getMediumIntTypeDeclarationSQL() not defined
        // return $platform->getMediumIntTypeDeclarationSQL($fieldDeclaration);

        // _getCommonIntegerTypeDeclarationSQL() protected, so cant call it
        // return 'MEDIUMINT' . $platform->_getCommonIntegerTypeDeclarationSQL($fieldDeclaration);

        // call the generation of Integer type, prepend the missing word MEDIUM
        return 'MEDIUM' . $platform->getIntegerTypeDeclarationSQL($fieldDeclaration);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        // This is executed when the value is read from the database. Make your conversions here, optionally using the $platform.
        return $value === null ? null : (int) $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        // This is executed when the value is written to the database. Make your conversions here, optionally using the $platform.
        return $value === null ? null : (int) $value;
    }

    public function getName()
    {
        return self::NAME;
    }
}

The Laravel framework provides the ability to declare custom types once we've created the corresponding classes for them. To achieve this, we need to follow the following pattern in the config/database.php file:

use App\Database\Types\MediumInteger;

'dbal' => [
    'types' => [
        MediumInteger::NAME => MediumInteger::class,
    ],
],

More information

@rozsazoltan
Copy link

Starting with Laravel 11, Doctrine DBAL is no longer necessary, which means the approach to addressing this question has also changed.

Laravel 11 introduces a built-in solution via its Schema facade, which now includes the long-awaited functionality for mediumInteger.


Source: Laravel migration to change an unsignedMediumInteger column - Stack Overflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants