0

I am developing a Laravel application. What I am trying to do now is that I am trying to add a new enum type to the existing enum column.

This is how enum column is created/ migrated in the first place.

$table->enum('type', [BlogType::IMAGE, BlogType::TEXT]);

Now I am trying to add a new enum type to that column creating a new migration like this.

Schema::table('blogs', function (Blueprint $table) {
            $table->enum('type', [BlogType::IMAGE, BlogType::TEXT, BlogType::VIDEO])->change();
        });

As you can see, I added a VIDEO blog type.

When I run the migration, I got the following error.

 Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

Then I came across this link, Laravel 5.1 Unknown database type enum requested.

I tried adding the following in the constructor of the migration class.

\Illuminate\Support\Facades\DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

Then I got the following error when I run the migration.

Unknown column type "enum" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

Now I am not renaming the column, I am just adding the new supported enum value. What would be the best solution and how can I do that, pelase?

3
  • Have you tried running it as raw queries instead of using DBAL, take a look at this answer here: stackoverflow.com/a/32967177/1457270 Commented Jul 31, 2019 at 9:36
  • this doesnt answer your question but you might want to use TINYINT instead, I personally dont like the ENUM column because of the way you need to define your options. With tinyint you can just use 0 to 128 (or 0 to 255 when unsigned) and resolve the integer to a string within php. Commented Jul 31, 2019 at 9:45
  • I do not want to use the explicit SQL query statement. That's why. What would be the better approach? Commented Jul 31, 2019 at 16:04

1 Answer 1

0

For now, we have only one solution: RAW query

public function up()
{
    DB::statement('
        ALTER TABLE `blogs` CHANGE COLUMN `type` `type` 
        ENUM("'.implode([BlogType::IMAGE, BlogType::TEXT, BlogType::VIDEO], '", "').'")
        NOT NULL
        DEFAULT "'.BlogType::IMAGE.'"
    ');
}

Where blogs is table name and type column name.

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

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.