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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| // 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);
}
}
|
Since Laravel 8, you can register it directly in the config file, but this only supports addType, not overrideType:
1
2
3
4
5
6
7
8
| // config/database.php
return [
'dbal' => [
'types' => [
'enum' => Doctrine\DBAL\Types\StringType::class
]
]
];
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| // 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');
}
}
}
}
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| 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
|