9

I have a MySQL database with a table called user_level_attempt. That table has a ENUM type column with ['PROGRESSED', 'STOPPED', 'COMPLETED'] values. I need to write a migration to add another value (let's say 'PASSED') to that column. After adding, it'll look like this, ['PROGRESSED', 'STOPPED', 'COMPLETED', 'PASSED]. How can I do that in Laravel? I tried the following solution but it doesn't seem like a good practice/solution.

 /**
         * Schema table name to migrate
         * @var string
         */
        public $set_schema_table = 'bt_user_level_attempt';


        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table($this->set_schema_table, function ($table) {
                $table->dropColumn('status');
            });

            Schema::table($this->set_schema_table, function ($table) {
                $table->enum('status', ['PROGRESS', 'STOPPED', 'COMPLETED', 'PASSED'])->default('PROGRESS')->after('effective_time_spend');
            });
        }

/**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table($this->set_schema_table, function ($table) {
            $table->dropColumn('status');
        });

        Schema::table($this->set_schema_table, function ($table) {
            $table->enum('status', ['PROGRESS', 'STOPPED', 'COMPLETED'])->default('PROGRESS')->after('effective_time_spend');
        });
    }

Thank you.

5 Answers 5

8

After all, I figure out to find a solution. Thanks to all fellows for enlightening me. :)

/**
     * Schema table name to migrate
     * @var string
     */
    public $set_schema_table = 'bt_user_level_attempt';


    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED', 'PASSED') NOT NULL DEFAULT 'PROGRESS'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED') NOT NULL DEFAULT 'PROGRESS'");
    }
Sign up to request clarification or add additional context in comments.

Comments

5

You should try with DB::statement method:

Use the DB::statement method:

DB::statement("ALTER TABLE ".$this->set_schema_table." CHANGE COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED','PASSED') NOT NULL DEFAULT 'PROGRESS'");

2 Comments

CHANGE is not working. Instead of CHANGE, I tried MODIFY and it worked!
you can do the same to my complete solution as well ;)
1

Please refer to documentation:

Only the following column types can be "changed": bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger and unsignedSmallInteger.

So the ENUM cannot be modified using simple migration syntax. But you can migrate your column using a custom statement:

DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED','PASSED') NOT NULL DEFAULT 'PROGRESS'");

Comments

1

Add this code in your migration file before the schema.

public function __construct()
    {
        \Illuminate\Support\Facades\DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
    }

Comments

-3

try something like :

edit

$table->enum('converted', array('yes','no'))->default('no');

1 Comment

This creates/modifies column of VARCHAR type (with CHECK constraint), not ENUM type.

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.