2014-11-05 125 views
1

我已经使用了php artisan migrate:make add_something_to_to_user_table --table=usersLaravel迁移:回滚添加和删除表中的列

和编码

Schema::table('users', function(Blueprint $table) 
    { 
     $table->string('description1'); 
     $table->string('description2'); 
     $table->string('description3'); 
    }); 

,并增加了三个领域,并给php artisan migrate和领域得到了保存到数据库

还发现migration table更新与行2014_11_05_145536_add_something_to_to_user_table

现在当我使用php artisan migrate:rollback

2014_11_05_145536_add_something_to_to_user_table行中的迁移表丢失,但列添加到用户表保持不变

为什么它没有在使用上表中的导致错误删除场php artisan migrate再次...

+0

你有没有正确设置了'向下()'在迁移类的方法? – Bogdan 2014-11-05 15:25:28

回答

9

你应该有你迁移down()方法看起来应该像这样的:

public function down() 
{ 
    Schema::table('users', function($table) 
    { 
     $table->dropColumn(array('description1', 'description2', 'description3')); 
    }); 
} 

这将在回滚时调用,并负责删除由迁移添加的列。

+0

我是否也必须在同一个文件中的'down()'中的'up()'函数中提及所有列? – Ronser 2014-11-05 15:33:38

+0

是的,他们应该在同一个文件中。在迁移类中,“up”方法将应用更改,“down”方法将撤消它们。 – Bogdan 2014-11-05 15:34:45

+2

请记住,如果您已经运行了'artisan migrate:rollback'命令,并且没有'down'方法设置,那么您必须在重新运行迁移之前手动删除列(这就是为什么在尝试迁移时遇到错误调用'migrate',因为它试图在已经存在的时候添加3列)。 – Bogdan 2014-11-05 15:37:18

-1

添加下来公共功能减退用户表时回滚..

public function down() 
{ 
    Schema::drop('users'); 
}