Search code examples
databaselaraveldatabase-migration

How to Disable Enum Check In Laravel


I want to add another new enum column in the table, but not able to migrate, as it says column already exists.

Migration

class DesignationColumnNullableInUserTable extends Migration
{
    public function __construct()
    {
        \Illuminate\Support\Facades\DB::getDoctrineSchemaManager()
        ->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
        Type::addType('enum', \Doctrine\DBAL\Types\StringType::class);
    }

    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->enum('designation', ['Lawyer', 'Freelancer', 
                'Corporate secretary', 'Immigration Consultant'])
             ->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('user', function (Blueprint $table) {
            $table->dropIfExists('designation');
        });
    }
} 

I have also made some changes in another migration with a foreign key.

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

How can I fix this issue for the enum check?


Solution

  • Laravel does not support modifying enum columns, so instead use a raw statement:

    public function up() {
        DB::statement("ALTER TABLE users MODIFY COLUMN designation ENUM('Lawyer', 'Freelancer', 'Corporate secretary', 'Immigration Consultant')");
    }
    

    For MySQL you should be able to use the following:

    // up
    DB::statement("ALTER TABLE users CHANGE designation designation ENUM('Lawyer', 'Freelancer', 'Corporate secretary', 'Immigration Consultant')");