How can I modify a migration in Laravel?

。_饼干妹妹 提交于 2020-04-05 13:55:12

问题


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:

.
.
    $table->string('error_message')->nullable();
.
.

But when I execute php artisan migrate again, it says:

Nothing to migrate.

How can I apply the new version of the migration?


回答1:


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:

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:

php artisan migrate:rollback --step=5

See more about Modifying columns with Migration




回答2:


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.




回答3:


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.

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

composer require doctrine/dbal



回答4:


There are 2 ways to do this:

  1. 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.
  2. 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 require composer require doctrine/dbal in your composer)




回答5:


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!

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


来源:https://stackoverflow.com/questions/41205844/how-can-i-modify-a-migration-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!