Ryan F
Ryan F

Reputation: 55

Renaming column in Laravel migration, forcing lowercase

I am trying to run a migration renaming a column on one of my tables. Whenever I try to rename the column, it gives me the error below.

"There is no column with name 'column_name_id' on table 'Table-Name.'"

However, the reason there is no column 'column_name_id' is that it is supposed to be 'Column_Name_ID' case sensitive like I wrote it.

I have tried to use strtoupper(). I have also used backticks to see if that helped. No joy.

I'm not sure whether or not this is an issue with my DB settings, but I've used renameColumn before with no problems.

public function up()
    {
        Schema::table('`Table-Name`', function (Blueprint $table) {
            $table->renameColumn('Column_Name_ID', 'Column_Name');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('`Table-Name`', function (Blueprint $table) {
            $table->renameColumn('Column_Name', 'Column_Name_ID');
        });
    }

Upvotes: 1

Views: 1230

Answers (2)

Mahdi Aslami Khavari
Mahdi Aslami Khavari

Reputation: 1983

As said in issue use double-quote for names.

Schema::table('`Table-Name`', function (Blueprint $table) {
    $table->renameColumn('"Column_Name_ID"', '"Column_Name"');
});

Upvotes: 1

Ryan F
Ryan F

Reputation: 55

What I ended up doing because I wont spend too long trying to resolve this issue is using a raw statement.

public function up()
   {
       DB::statement('alter table `Table-Name` change column `Column_Name_ID` `Column_Name` integer(11) null');
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   ///

Upvotes: 0

Related Questions