EU Cookie Consent

To use this Website we are using Cookies and collecting some Data. To be compliant with the EU GDPR we give you to choose if you allow us to use certain Cookies and to collect some Data.

Essential Data

The Essential Data is needed to run the Site you are visiting technically. You can not deactivate them.

Laravel

Changing enum column in Laravel migration

Max Hutschenreiter -

Changing enum type columns is currently not supported in Laravel, and if you try to run something like:
$table->enum('example_enum_column', array('enum1','enum2','enum3+'))->nullable()->change();
You are about to get the following exception:

Doctrine\DBAL\DBALException : Unknown database type enum requested, Doctrine\DBAL\Platforms\MariaDb1027Platform may not support it.

at /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php:436
432|
433| $dbType = strtolower($dbType);
434|
435| if (! isset($this->doctrineTypeMapping[$dbType])) {
436| throw new DBALException(‘Unknown database type ‘ . $dbType . ‘ requested, ‘ . static::class . ‘ may not support it.’);
437| }
438|
439| return $this->doctrineTypeMapping[$dbType];
440| }

Exception trace:

1 Doctrine\DBAL\Platforms\AbstractPlatform::getDoctrineTypeMapping(“enum”)
/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php:128

2 Doctrine\DBAL\Schema\MySqlSchemaManager::_getPortableTableColumnDefinition()
/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php:810
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');"); 
     }
Tags: Laravel · Enum · Enum Migration · Laravel Enum

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • Happy Birthday Ognjen! And marry Christmas to all other 🎄#notacakeshop - #

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan 🥂 - #

  • #happybirthday Lidija 🍾 - #

  • Say hi 👋 to our newest team member ☕️ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday 🎂 Miloš - #

  • Happy Birthday 🎂Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday 🎂 Ognjen - #

  • Wish you all a merry Christmas 🎄🎁 - #

  • #happybirthday Luka! - #

  • https://communian.com Triff dich mit Menschen aus deiner Umgebung die deine Interessen teilen :) Wir sind gerade live gegangen und freuen uns über jede neue Community die zu uns kommt ❤️ - #

© 2021 42coders All rights reserved.