I'm using PHP 7.3|8.0, Laravel 8.75, Doctrine DBAL 3.6 and MySQL.
I want to change the data type of a column from integer to enum. I also added custom enum type following this guide https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/cookbook/mysql-enums.html.
<?php
use App\Types\AccountEnumType;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeAccountTypeColumnInAccountTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$connectionParams = [
'driver' => env('DB_DRIVER'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'dbname' => env('DB_DATABASE'),
'user' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
];
$connection = DriverManager::getConnection($connectionParams);
Type::addType('enum', AccountEnumType::class);
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
Schema::table('accounts', function (Blueprint $table) {
$table->enum('account_type', config('enums.account_type'))->default('SAVINGS')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('accounts', function (Blueprint $table) {
$table->integer('account_type')->unsigned()->change();
});
}
}
The migration file is working fine when migrating but when I rollback I got this exception.
Doctrine\DBAL\Exception
Unknown column type "account" 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 vendor/doctrine/dbal/src/Exception.php:115
111▕ }
112▕
113▕ public static function unknownColumnType(string $name): self
114▕ {
➜ 115▕ return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .
116▕ 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
117▕ 'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
118▕ 'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .
119▕ 'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
+16 vendor frames
17 database/migrations/2023_07_07_091358_change_account_type_column_in_account_table.php:47
Illuminate\Support\Facades\Facade::__callStatic()
+22 vendor frames
40 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
I tried using raw SQL statement its working fine
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement("ALTER TABLE accounts MODIFY COLUMN account_type INT");
}
But how to do it with schema builder?