使用 Laravel,您可以在迁移过程中更改列名吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13364002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With Laravel, can you change a column name as part of a migration?
提问by Steve Abraham
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?
几乎就像标题所说的那样;作为迁移的一部分,我需要更改列名,是否可以完成,如果可以,如何完成?
回答by Jason Lewis
Laravel 3
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.
是的,它可以完成,但只能使用原始查询。某些 DBMS 不支持更改列名,因此决定不实现此类功能,因为对于某些 DBMS,它会失败。
So don't forget that you can leverage raw queries using DB::raw()
within migrations. You can change your column names that way.
所以不要忘记您可以DB::raw()
在迁移中使用原始查询。您可以通过这种方式更改列名称。
Laravel 4
Laravel 4
In Laravel 4.1 you must add doctrine/dbal
as a dependency in composer.json
.
在 Laravel 4.1 中,您必须doctrine/dbal
在composer.json
.
"doctrine/dbal": "2.4.*"
Once you've run composer update
you can now use the renameColumn
method.
运行后,composer update
您现在可以使用该renameColumn
方法。
Schema::table('users', function($table)
{
$table->renameColumn('location', 'address');
});
This will rename the location
column to address.
这会将location
列重命名为address.
回答by ollieread
The renameColumn() method isn't available by default in Laravel 4.1, see release notes:
在 Laravel 4.1 中默认情况下 renameColumn() 方法不可用,请参阅发行说明:
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.
如果您在迁移中使用 renameColumn 函数,则需要将学说/dbal 依赖项添加到您的 composer.json 文件中。默认情况下,这个包不再包含在 Laravel 中。
回答by Aken Roberts
Note that as of Laravel 4, the DB Schema doesallow for column renaming via $table->renameColumn('from', 'to')
.
请注意,从 Laravel 4 开始,DB 模式确实允许通过$table->renameColumn('from', 'to')
.
回答by Nicolay77
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:
在renameColumn()
Laravel 4的功能被修复之前(现在它失败并There is no column with name
出现错误),你可以使用这样的东西:
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');
});
回答by abbood
I tried using the syntaxmentioned in laravel's schema builder like so:
我尝试使用laravel 的架构构建器中提到的语法,如下所示:
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:
虽然这在我的 localhost 机器上运行得很好..但是,它因以下错误而窒息:
[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 outthis 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:
做一些研究……结果这个错误与字符集和排序规则有关。 ).. 我只是像这样使用 DB::RAW:
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::Rawcan do whatever migration you want without running into the limitations of Laravel..
关键是DB::Raw可以做任何你想做的迁移,而不会遇到 Laravel 的限制。
回答by Andrew E
According to https://github.com/laravel/laravel/issues/2146, renameColumn was removed and there is no fix available:
根据https://github.com/laravel/laravel/issues/2146, renameColumn 被删除并且没有可用的修复:
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
对于 Laravel 4.1 和 dbal 2.2,这可以在 vendor/doctrine/dbal/UPGRADE 中找到