php 如何使用laravel 5迁移在表中添加列而不会丢失其数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36718773/
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
How to add column in a table using laravel 5 migration without losing its data?
提问by Eli
I have an existing database table and I want to add column on it. However, as I run the php artisan migrate
command, it says nothing to migrate. But I already add a Schema for adding table columns. I have read some articles and links that I should run the php artisan migrate:refresh
first before the new columns to be added.The problem is, it will erase my existing data in my table. Is there any way I could perform the migration and successfully add columns in my table without deleting my data? Please help me with this. Thanks a lot. Here is my migration code.
我有一个现有的数据库表,我想在它上面添加列。但是,当我运行该php artisan migrate
命令时,它没有说明要迁移。但是我已经添加了一个用于添加表列的架构。我已经阅读了一些文章和链接,我应该php artisan migrate:refresh
在添加新列之前先运行这些文章和链接。问题是,它会删除我表中的现有数据。有什么方法可以执行迁移并在我的表中成功添加列而不删除我的数据?请帮我解决一下这个。非常感谢。这是我的迁移代码。
public function up()
{
//
Schema::create('purchase_orders', function(Blueprint $table){
$table->increments('id');
$table->string('po_code');
$table->text('purchase_orders');
$table->float('freight_charge');
$table->float('overall_total');
$table->timestamps();
});
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('purchase_orders');
}
I want to add column shipped_via
and terms
in my purchase_orders
table.
我想在我的表中添加列shipped_via
和。terms
purchase_orders
回答by Abhishek
Use below command to modify the existing table
使用以下命令修改现有表
php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders
use --create
for creating the new table and --table
for modifying the existing table.
使用--create
创建新表和--table
修改现有的表。
Now a new migration file will be created. Inside the up()
function in this file add these line
现在将创建一个新的迁移文件。up()
在此文件的函数内添加这些行
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
And then run php artisan migrate
然后运行 php artisan migrate
回答by Jirennor
Laravel has a table in your database where it keeps track of all the migrations that are already executed. So by only changing the migration file Laravel will not automatically rerun that migration for you. Cause the migration is already executed by Laravel.
Laravel 在你的数据库中有一个表,它跟踪所有已经执行的迁移。因此,仅更改迁移文件 Laravel 不会自动为您重新运行该迁移。因为 Laravel 已经执行了迁移。
So the best thing to do is to just create a new migration and put the piece of code in it you already have (you were on the right track!).
所以最好的办法就是创建一个新的迁移并将你已经拥有的代码片段放入其中(你在正确的轨道上!)。
public function up()
{
//
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
You don't need to populate the down function case the table will be dropped by your current purchase_orders migration.
您不需要填充 down 函数,如果您当前的 purchase_orders 迁移将删除该表。
To migrate the new migration just run:
要迁移新的迁移,只需运行:
php artisan migrate