3

Pretty much just what the title says; I need to change a column name as part of a migration, can that be done and if so, how?

Charles
  • 50,943
  • 13
  • 104
  • 142
Steve Abraham
  • 370
  • 1
  • 2
  • 9
  • There is no description about rename column in [Schema Builder docs](http://laravel.com/docs/database/schema) and no code contains 'CHANGE' in grammers files. I think it is not possible with Laravel 3. – aykut Nov 13 '12 at 16:10
  • Thanks for looking. I find it strange, it seems such an obvious thing to want to do as part of a migration... – Steve Abraham Nov 13 '12 at 16:28

6 Answers6

9

Laravel 3

Yes it can be done, but only with raw queries. Some DBMS don't support the changing of column names so it was decided to not implement such functionality since for some DBMS it would fail.

So don't forget that you can leverage raw queries using DB::raw() within migrations. You can change your column names that way.

Laravel 4

In Laravel 4.1 you must add doctrine/dbal as a dependency in composer.json.

"doctrine/dbal": "2.4.*"

Once you've run composer update you can now use the renameColumn method.

Schema::table('users', function($table)
{
    $table->renameColumn('location', 'address');
});

This will rename the location column to address.

Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
3

The renameColumn() method isn't available by default in Laravel 4.1, see release notes:

If you are using the renameColumn function in your migrations, you will need to add the doctrine/dbal dependency to your composer.json file. This package is no longer included in Laravel by default.

http://laravel.com/docs/releases

ollieread
  • 6,018
  • 1
  • 20
  • 36
2

Note that as of Laravel 4, the DB Schema does allow for column renaming via $table->renameColumn('from', 'to').

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40
1

Until the renameColumn() functionality of Laravel 4 is fixed (right now it fails with There is no column with name error), you can use something like this:

Schema::table('table_name', function(Blueprint $table) {
    $table->string('new_name')->after('some_other_column_name');
});
DB::table('table_name')->update(array('new_name' => DB::raw('old_name')));
Schema::table('table_name', function(Blueprint $table) {
    $table->dropColumn('old_name');
});
Nicolay77
  • 2,085
  • 25
  • 20
1

I tried using the syntax mentioned in laravel's schema builder like so:

public function up()
{
    if (Schema::hasColumn('lesson_plans', '`desc`'))
    {
        Schema::table('lesson_plans', function(Blueprint $table)
        {
                $table->renameColumn('`desc`', 'comment');
        });
    }
}
    ..

while this worked just fine on my localhost machine.. however, it choked on prod with the following error:

[PDO Exception]
SQLSTATE [HY000]: General error: 1366 incorrect string value: '\xD9\x8A\xD8\xB1\xD8\xAC\...' for column 'comment' at row 115

doing some research.. it turns out this error has something to do with the character set and collation.. I couldn't find anything on the web that shows me how to specify those guys using laravel's schema builder (the examples are pretty scarce frankly).. I just used DB::RAW like so:

public function up()
{
    if (Schema::hasColumn('lesson_plans', '`desc`'))
    {
        DB::select(DB::raw('alter table lesson_plans change `desc` `comment` longtext character set utf8 collate utf8_general_ci default null'));
    }
}

worked like a charm locally and on prod!

The point being is that DB::Raw can do whatever migration you want without running into the limitations of Laravel..

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246
1

According to https://github.com/laravel/laravel/issues/2146 , renameColumn was removed and there is no fix available:

public function renameColumn($oldColumnName, $newColumnName) {
    throw new DBALException("Table#**renameColumn() was removed**, because it drops and recreates the column instead. **There is no fix available**, because a schema diff cannot reliably detect if a column was renamed or one column was created and another one dropped.");
}

For Laravel 4.1 with doctrine/dbal 2.2, this can be found in vendor/doctrine/dbal/UPGRADE

rareyesdev
  • 2,337
  • 1
  • 25
  • 43
Andrew E
  • 158
  • 2
  • 4