php 如何在 Laravel 中修改迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41205844/
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 can I modify a migration in Laravel?
提问by stack
I'm trying to modify a existing migration. Here is my current migration class:
我正在尝试修改现有的迁移。这是我当前的迁移课程:
class CreateLogForUserTable extends Migration
{
public function up()
{
Schema::create('log_for_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('table_name');
$table->string('error_message');
$table->unsignedTinyInteger('error_code');
$table->timestamps();
});
}
public function down()
{
Schema::drop('log_for_user');
}
}
I've executed the php artisan migrate
command once. Now I need to add ->nullable()
method to the error_message
column. So I edited my migration, something like this:
我已经执行了php artisan migrate
一次命令。现在我需要向列添加->nullable()
方法error_message
。所以我编辑了我的迁移,如下所示:
.
.
$table->string('error_message')->nullable();
.
.
But when I execute php artisan migrate
again, it says:
但是当我php artisan migrate
再次执行时,它说:
Nothing to migrate.
没什么好迁移的。
How can I apply the new version of the migration?
如何应用新版本的迁移?
回答by Saumya Rastogi
You should create a new migration using command:
您应该使用命令创建一个新的迁移:
php artisan make:migration update_error_message_in_log_for_user_table
Then, in that created migration class, add this line, using the change
method like this:
然后,在创建的迁移类中,使用如下change
方法添加这一行:
class UpdateLogForUserTable extends Migration
{
public function up()
{
Schema::table('log_for_user', function (Blueprint $table) {
$table->string('error_message')->nullable()->change();
});
}
public function down()
{
Schema::table('log_for_user', function (Blueprint $table) {
$table->string('error_message')->change();
});
}
}
To make these changes and run the migration, use the command:
要进行这些更改并运行迁移,请使用以下命令:
php artisan migrate
and to rollback the changes, use the command:
要回滚更改,请使用以下命令:
php artisan migrate:rollback
You may rollback a limited number of migrations by providing the step
option to the rollback command. For example, the following command will rollback the last five migrations:
您可以通过为step
rollback 命令提供选项来回滚有限数量的迁移。例如,以下命令将回滚最近的五次迁移:
php artisan migrate:rollback --step=5
See more about Modifying columns with Migration
查看有关使用迁移修改列的更多信息
回答by Alexey Mezenin
If your app is not in production and you seed your data, the best you can do is to run:
如果您的应用程序不在生产中并且您为数据播种,那么您能做的最好的事情就是运行:
php artisan migrate:refresh --seed
This command will drop all tables and recreate them. Then it will seed the data.
此命令将删除所有表并重新创建它们。然后它将播种数据。
If you will create additional migrations for each change during development, you'll end up with hundreds of migrations classes.
如果您在开发过程中为每个更改创建额外的迁移,您最终将拥有数百个迁移类。
回答by Amit Gupta
You can use the change
method, it allows you to modify some existing column types to a new type or modify the column's attributes.
您可以使用该change
方法,它允许您将一些现有的列类型修改为新类型或修改列的属性。
For example modify a column to be nullable:
例如将一列修改为可以为空:
Schema::table('log_for_user', function ($table) {
$table->string('error_message')->nullable()->change();
});
But first of all you'll need the doctrine/dbal
package
但首先你需要这个doctrine/dbal
包
composer require doctrine/dbal
回答by aceraven777
There are 2 ways to do this:
有两种方法可以做到这一点:
- Run
php artisan migrate:refresh
. This will rollback all your migrations and migrate all your migrations. If you run this command, all the data inserted in your database will be lost. Run
php artisan make:migration enter_your_migration_name_here
. Then insert this in your migration:$table->string('error_message')->nullable()->change();
Then run
php artisan migrate
to make your table changes. (Take note that when you do this, you have requirecomposer require doctrine/dbal
in your composer)
- 运行
php artisan migrate:refresh
。这将回滚您的所有迁移并迁移您的所有迁移。如果你运行这个命令,所有插入到你数据库中的数据都会丢失。 运行
php artisan make:migration enter_your_migration_name_here
。然后将其插入到您的迁移中:$table->string('error_message')->nullable()->change();
然后运行
php artisan migrate
以更改您的表。(请注意,当你这样做时,你composer require doctrine/dbal
的作曲家有要求)
回答by miken32
There is one more option. Roll back the migration, edit the file, and run it again.
还有一种选择。回滚迁移,编辑文件,然后再次运行。
This is a fairly common thing to do on a local development server while you're working out the bugs in a new piece of code. Using the method from the accepted answer, you might end up with 17 migrations for creating a single table!
在本地开发服务器上处理新代码中的错误时,这是很常见的事情。使用已接受答案中的方法,您最终可能会进行 17 次迁移以创建单个表!
php artisan migrate
# realize you've made an error
php artisan migrate:rollback
# edit your migration file
php artisan migrate
The number of steps back to take can be specified on the command line if needed.
如果需要,可以在命令行上指定后退的步骤数。
# undo the last 3 migrations
php artisan migrate:rollback --step=3
Or you can specify a particular migration that needs undoing.
或者您可以指定需要撤消的特定迁移。
# undo one specific migration
php artisan migrate:rollback --path=./database/migrations/2014_10_12_100000_create_users_table.php