Reputation: 370
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?
Upvotes: 3
Views: 10917
Reputation: 18665
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.
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.
Upvotes: 9
Reputation: 184
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
Upvotes: 1
Reputation: 23548
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..
Upvotes: 1
Reputation: 6301
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
Upvotes: 3
Reputation: 2154
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');
});
Upvotes: 1
Reputation: 13457
Note that as of Laravel 4, the DB Schema does allow for column renaming via $table->renameColumn('from', 'to')
.
Upvotes: 2