18
votes

I am using Laravel 4.2. I have the following library loaded in my composer.json

"doctrine/dbal": "2.4.*",

I created the following migration:

class RenameDeliveryNotesColumnOnOrderHeaderTable extends Migration {

    public function up()
    {
        Schema::table('order_header', function(Blueprint $table)
        {
            $table->renameColumn('delivery_notes', 'packing_notes');
        });
    }

}

Where delivery_notes column type is text.

When I run the migration, I get the following error:

[Doctrine\DBAL\DBALException] Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

Any idea why I am getting this error? How should I go about fixing this? I need to rename a column in my table. Are there any alternative way to rename the column?

8
I believe this error is caused by another migration, not exactly this one. Take a look at the migrations you have and havent been ran yet. - Ivanka Todorova
I have no other migrations left to run prior to this one. already checked. - Latheesan

8 Answers

16
votes

Laravel's documentation says that:

Note: Renaming enum column types is not supported.

Here: https://github.com/laravel/framework/issues/1186

You can find some workarounds about this issue. And since you said that this column is not enum, take a look at @upngo's comment:

"...The issue is renaming ANY column on a table that has an enum."

Also I found this article that focuses on this issue and suggest an option that might help you.

http://www.paulbill.com/110/laravel-unknown-database-type-enum-requested-doctrinedbalplatformsmysqlplatform-may-not-support-it

34
votes
DB::getDoctrineSchemaManager()
    ->getDatabasePlatform()
    ->registerDoctrineTypeMapping('enum', 'string');

This works for me on Laravel 5.1

9
votes

I met this problem in Laravel version 5.1.19 (LTS). This is actual for earlier versions too. I wanted to inform you as I resolved the issue base on previous comments.

First of all, I tried next code in my migration file:

$table->renameColumn('column_name');

But after command php artisan migrate, I got next error:

[Symfony\Component\Debug\Exception\FatalErrorException] Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found

As you know DBAL was removed from the laravel core and we need add it to the composer.json.(For example:"require": {"doctrine/dbal": "2.5.1"}). I set DBAL as required and tried again to do migrate command but got next error:

[Doctrine\DBAL\DBALException]
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

Then I tried next raw sql in my migration file: For up():

DB::statement("ALTER TABLE `table_name` CHANGE `old_column_name` `new_column_name` ENUM('first value', 'second_value', ...) DEFAULT 'first_value' AFTER `some_field`");

For down():

DB::statement("ALTER TABLE `table_name` CHANGE `new_column_name` `old_column_name` ENUM('first value', 'second_value', ...) DEFAULT 'first_value' AFTER `some_field`");

and it works.

P.S. For renaming other fields in the table which contains an enum field we should use same schema with raw sql as was written in previous comments.

5
votes

You can add custom constructor to migration and explain to Doctrine that enum should be treated like string.

public function __construct(\Doctrine\DBAL\Migrations\Version $version)
{
    parent::__construct($version);

    $this->platform->registerDoctrineTypeMapping('enum', 'string');
}
3
votes

Here is the answer for Laravel 5.2.45+ (might work in 5.1 as well, have not tested or checked yet, please let me know so I can update this question.)

Add this line in your up method:

Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

Something like this:

public function up()
{
    Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
    Schema::table('users', function (Blueprint $table) {
         $table->text('bio')->change();
    });
}
1
votes

I had the same problem with Laravel 5.1 and PostGres. So basically I used the DB::statement to create the ENUM and solve the problem:

DB::statement("CREATE TYPE e_users AS ENUM('data1','data2')");

And then:

DB::statement("ALTER TABLE users ADD COLUMN column e_users");

1
votes

Though the original author had issues with Laravel 4, this can safely be fixed in Laravel 5 by bumping the version of doctrine/dbal in your composer.json to ^2.6, as it was fixed in this PR on release 2.6.0

Make sure to check for compatibility-breaking changes in the release changelog

0
votes

Currently, there is no legal solution for this problem except avoiding enums, but there is a workaround:

Create migration with the following:

public function up()
{
    DB::statement("ALTER TABLE `table` CHANGE `example_enum_column` `example_enum_column` ENUM('enum1', 'enum2', 'enum3+');");
}

And that will do the trick with the ENUM update you are looking for. Additionally, you can create handle down functions to revert field status as it used to be:

 public function down() 
 {
     DB::statement("ALTER TABLE `table` CHANGE `example_enum_column` `example_enum_column` ENUM('enum1', 'enum2');"); 
 }