20

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?

2
  • 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. Commented Mar 20, 2015 at 12:38
  • I have no other migrations left to run prior to this one. already checked. Commented Mar 20, 2015 at 16:01

8 Answers 8

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

This works for me on Laravel 5.1

Sign up to request clarification or add additional context in comments.

5 Comments

Do you put it in some kind of bootstrap file?
nope, you should run this from your migration file, best in a construct method.
Be aware that this makes the column work as a string now, and not an enum, therefore allowing any string to be saved.
@Meriw When the column to be renamed is not itself an ENUM(), as is the case in the OP's question, this method does not change any ENUM() column on the table, to a string type or otherwise; it affects only the column to be renamed.
@BenJohnson awesome stuff
16

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

Comments

10

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.

Comments

4

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');
}

2 Comments

Seems not workable in current versions? I am on Laravel 5.1
same here on Laravel 5.2
4

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();
    });
}

Comments

1

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");

Comments

1

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

Comments

1

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');"); 
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.