Laravel: How to Use Migrations to Update a Table That Has An Enum Field

** Disclaimer: The only version of Laravel that this has actually been used/tested is 5.2. Seems the issue still exists in the new 5.3 release – but it’s likely due more to the doctrine/dbal package than the Laravel core.

For update migrations, Laravel relies on the doctrine/dbal package to examine the columns and create the SQL query to actually do the update. Unfortunately, this package doesn’t support altering tables that have an enum field on them. This doesn’t just mean that you can’t edit that particular column – it means you can’t edit any column for a table that has at least one enum field.

So, how do we fix this? It’s actually pretty simple. Doctrine just has a problem because it doesn’t know how to properly handle the field as “enum” – so, we just need to convert (in the eyes of doctrine) all our enums to string fields while it’s doing the update.

Important: this does NOT convert your enum fields to varchar, it just changes how Doctrine see the field and interacts with this. It will not alter the enum fields at all.

Alright, here it is:

public function __construct()
    DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

All you have to do is paste that code into the class for your migration and you’re good to go. All it does is changes the type mapping on enum fields from “enum” to “string” to stop Doctrine from pushing an error. Like I said before, this doesn’t actually change your database, it just changes how Doctrine maps enum fields

Got a better way to do it? Let me know in the comments!

View Comments
There are currently no comments.