1

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');
        });
    }
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ryan F
  • 55
  • 1
  • 11
  • What is your OS? Please check this link https://stackoverflow.com/questions/2009005/are-column-and-table-name-case-sensitive-in-mysql as it shows that unix has case sensitive naming for fields name but window doesn't care about that. May be it is an issue in your case? – Rohit Mittal Apr 03 '19 at 02:45
  • I know windows doesn't care, but it has never turned it lowercase for me before. Local is windows, but I push to a unix server – Ryan F Apr 03 '19 at 03:02
  • Try double quotes "Table-Name" – Vinay Apr 03 '19 at 03:03
  • No that did not work. – Ryan F Apr 03 '19 at 03:13
  • In config/database.php add this line to mysql drive `'options' => [PDO:: CASE_NATURAL =>true],` – Chintan7027 Apr 03 '19 at 05:04

2 Answers2

1

As said in issue use double-quote for names.

Schema::table('`Table-Name`', function (Blueprint $table) {
    $table->renameColumn('"Column_Name_ID"', '"Column_Name"');
});
Mahdi Aslami Khavari
  • 1,755
  • 15
  • 23
0

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
    */
   ///
Ryan F
  • 55
  • 1
  • 11