Laravel db migration - renameColumn error - Unknown database type enum requested

前端 未结 8 843
暗喜
暗喜 2020-12-10 11:53

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

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

I c

相关标签:
8条回答
  • 2020-12-10 11:59

    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');
    }
    
    0 讨论(0)
  • 2020-12-10 12:01

    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.

    0 讨论(0)
  • 2020-12-10 12:02

    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

    0 讨论(0)
  • 2020-12-10 12:04

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

    0 讨论(0)
  • 2020-12-10 12:05

    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 讨论(0)
  • 2020-12-10 12:17

    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');"); 
     }
    
    0 讨论(0)
提交回复
热议问题