Laravel 的重命名列迁移改变了列的位置
Laravel's rename column migration altering the position of column
我有我正在做的迁移文件:
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_old_name', 'col_new_name');
});
运行 此迁移使用新的列名称更新了 table_xyz
,但也更改了列在数据库中的位置(将列放在末尾)
为了避免我这样做:
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_old_name', 'col_new_name')
->after('col_to_be_after')
});
这也没有产生预期的结果。
我错过了什么?
尝试按照以下步骤操作,希望您遗漏了一些您的代码看起来不错的东西。
删除现有迁移并添加新迁移
rm database/migrations/your-migration-file`
php artisan make:migration rename_col_old_name_table_xyz_table --table=table_xyz
在up()
和down()
中添加如下代码
public function up()
{
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_old_name', 'col_new_name');
});
}
And down() function with this,
public function down()
{
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_new_name', 'col_old_name');
});
}
有关详细文档,请查看 Renaming columns in migrations
Since you are renaming column then it should not add at the end, so I don't think you need add ->after('col_to_be_after')
如果您想保留数据,这是一种方法:
public function up()
{
//Give the moving column a temporary name:
Schema::table('table_xyz', function($table)
{
$table->renameColumn('col_new_name', 'col_old_name');
});
//Add a new column with the regular name:
Schema::table('table_xyz', function(Blueprint $table)
{
$table->string('col_new_name')->after('col_to_be_after');
});
//Copy the data across to the new column:
DB::table('table_xyz')->update([
'col_new_name' => DB::raw('col_old_name')
]);
//Remove the old column:
Schema::table('table_xyz', function(Blueprint $table)
{
$table->dropColumn('col_old_name');
});
}
你不能这样做,
$table->renameColumn('col_old_name', 'col_new_name')
->after('col_to_be_after')
renameColumn()
returns Illuminate\Support\Fluent
但 ->after()
在 Illuminate\Database\Schema\ColumnDefinition
之下。所以这就是为什么你的代码不起作用。
在不丢失数据的情况下,您可能需要:
在特定位置创建一个新的:$table->string('xyx')->after('qwe');
将旧的数据复制到新的
删除旧列
我有我正在做的迁移文件:
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_old_name', 'col_new_name');
});
运行 此迁移使用新的列名称更新了 table_xyz
,但也更改了列在数据库中的位置(将列放在末尾)
为了避免我这样做:
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_old_name', 'col_new_name')
->after('col_to_be_after')
});
这也没有产生预期的结果。
我错过了什么?
尝试按照以下步骤操作,希望您遗漏了一些您的代码看起来不错的东西。
删除现有迁移并添加新迁移
rm database/migrations/your-migration-file`
php artisan make:migration rename_col_old_name_table_xyz_table --table=table_xyz
在up()
和down()
public function up()
{
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_old_name', 'col_new_name');
});
}
And down() function with this,
public function down()
{
Schema::table('table_xyz', function (Blueprint $table) {
$table->renameColumn('col_new_name', 'col_old_name');
});
}
有关详细文档,请查看 Renaming columns in migrations
Since you are renaming column then it should not add at the end, so I don't think you need add
->after('col_to_be_after')
如果您想保留数据,这是一种方法:
public function up()
{
//Give the moving column a temporary name:
Schema::table('table_xyz', function($table)
{
$table->renameColumn('col_new_name', 'col_old_name');
});
//Add a new column with the regular name:
Schema::table('table_xyz', function(Blueprint $table)
{
$table->string('col_new_name')->after('col_to_be_after');
});
//Copy the data across to the new column:
DB::table('table_xyz')->update([
'col_new_name' => DB::raw('col_old_name')
]);
//Remove the old column:
Schema::table('table_xyz', function(Blueprint $table)
{
$table->dropColumn('col_old_name');
});
}
你不能这样做,
$table->renameColumn('col_old_name', 'col_new_name')
->after('col_to_be_after')
renameColumn()
returns Illuminate\Support\Fluent
但 ->after()
在 Illuminate\Database\Schema\ColumnDefinition
之下。所以这就是为什么你的代码不起作用。
在不丢失数据的情况下,您可能需要:
在特定位置创建一个新的:
$table->string('xyx')->after('qwe');
将旧的数据复制到新的
删除旧列