Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 migration ENUM fails in MySQL

when I try to apply my migration, I get this error:

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

The migration is applied, enum column is created on database and I get the error, so I can't execute the nexts migrations because this migration throw this error.

In the server, I've MySQL version 5.7.17

This is the code of my migration:

class AddDocumentUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('document', 9)->unique();
            $table->enum('document_type', ['dni', 'nie', 'nif', 'cif']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('document');
            $table->dropColumn('document_type');
        });
    }
}

Thanks ;)

like image 412
Ivan Montilla Avatar asked Dec 19 '22 09:12

Ivan Montilla


1 Answers

Information related strictly to laravel can be found here. I highly advise you read the thread. This is NOT a laravel issue, it's been a bug in Doctrine since forever.

From the issue thread above, user henritoivar has an interesting idea.

Quoting here:

This worked for me in laravel 5.2 with doctrine/dbal@^2.5 . When you have an enum on your table and you want to change any of the columns on the table you will have to:

public function up()
{
   Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

    Schema::table('jobs', function(Blueprint $table)
    {
        $table->decimal('latitude', 10, 6)->nullable()->change();
    });
}

I have no idea if this will work for you, but it's worth a try.


I would have posted this as a comment, but it's a damn long read.

From the official Doctrine documents:

The type system of Doctrine 2 consists of flyweights, which means there is only one instance of any given type. Additionally types do not contain state. Both assumptions make it rather complicated to work with the Enum Type of MySQL that is used quite a lot by developers. When using Enums with a non-tweaked Doctrine 2 application you will get errors from the Schema-Tool commands due to the unknown database type “enum”. By default Doctrine does not map the MySQL enum type to a Doctrine type. This is because Enums contain state (their allowed values) and Doctrine types don’t.

Technically speaking this can be solved. See here. But that relates strictly to symfony, on which Laravel is based.


Laravel's docs also stated that it has a problem with enums:

Renaming any column in a table that also has a column of type enum is >not currently supported.


While this is not an answer, I hope it points you in the right direction or at least gives you an idea of what you're facing.


More related questions:

How to enable ENUMs in Symfony 2 / Doctrine

Laravel 5.1 Unknown database type enum requested

like image 184
Andrei Avatar answered Dec 29 '22 11:12

Andrei