DEV Community

Recca Tsai
Recca Tsai

Posted on • Originally published at recca0120.github.io

Fix Laravel Migration Unknown Database Type Enum Error

Originally published at recca0120.github.io

When running artisan migration, you encounter the Unknown database type enum requested error.

Cause

Laravel's migration uses Doctrine DBAL under the hood, and Doctrine DBAL doesn't recognize MySQL's enum type by default. You need to manually register enum as a type that Doctrine understands.

Method 1: Register in AppServiceProvider

Use Type::addType in AppServiceProvider to register enum as StringType:

// app/Providers/AppServiceProvider.php
namespace App\Providers;

use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        if (!Type::hasType('enum')) {
            Type::addType('enum', StringType::class);
        }

        // If you need to force override
        // Type::overrideType('enum', StringType::class);
    }
}
Enter fullscreen mode Exit fullscreen mode

Method 2: Configure in config/database.php (Laravel 8+)

Since Laravel 8, you can register it directly in the config file, but this only supports addType, not overrideType:

// config/database.php
return [
    'dbal' => [
        'types' => [
            'enum' => Doctrine\DBAL\Types\StringType::class
        ]
    ]
];
Enter fullscreen mode Exit fullscreen mode

When You See the MySQL57Platform Error

If the error message is Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it., the fix is completely different. Use registerDoctrineTypeMapping instead:

// app/Providers/AppServiceProvider.php
namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        if ($this->app->runningInConsole()) {
            /** @var AbstractPlatform $platform */
            $platform = DB::connection()
                ->getDoctrineConnection()
                ->getDatabasePlatform();

            if (!$platform->hasDoctrineTypeMappingFor('enum')) {
                $platform->registerDoctrineTypeMapping('enum', 'string');
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The two error messages look very similar, but the fixes are completely different. Pay attention to which one you're seeing.

Available Doctrine DBAL Types

Doctrine\DBAL\Types\ArrayType::class
Doctrine\DBAL\Types\AsciiStringType::class
Doctrine\DBAL\Types\BigIntType::class
Doctrine\DBAL\Types\BinaryType::class
Doctrine\DBAL\Types\BlobType::class
Doctrine\DBAL\Types\BooleanType::class
Doctrine\DBAL\Types\DateType::class
Doctrine\DBAL\Types\DateImmutableType::class
Doctrine\DBAL\Types\DateIntervalType::class
Doctrine\DBAL\Types\DateTimeType::class
Doctrine\DBAL\Types\DateTimeImmutableType::class
Doctrine\DBAL\Types\DateTimeTzType::class
Doctrine\DBAL\Types\DateTimeTzImmutableType::class
Doctrine\DBAL\Types\DecimalType::class
Doctrine\DBAL\Types\FloatType::class
Doctrine\DBAL\Types\GuidType::class
Doctrine\DBAL\Types\IntegerType::class
Doctrine\DBAL\Types\JsonType::class
Doctrine\DBAL\Types\ObjectType::class
Doctrine\DBAL\Types\SimpleArrayType::class
Doctrine\DBAL\Types\SmallIntType::class
Doctrine\DBAL\Types\StringType::class
Doctrine\DBAL\Types\TextType::class
Doctrine\DBAL\Types\TimeType::class
Doctrine\DBAL\Types\TimeImmutableType::class
Enter fullscreen mode Exit fullscreen mode

Top comments (0)