简体   繁体   中英

Add new column to database table with migrations fails with - Nothing to migrate

I just learned how to use Migrations. I successfully created a table with this schema:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('units', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->timestamps();
    });
}

Unfortunattely I missed the column path , so I've tried to add it. First I informed myselfe in the laravel documentation to learn how to add new columns.

From the documentation:

The table method on the Schema facade may be used to update existing tables.

My attempt:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('units', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->timestamps();
    });

    Schema::table('units', function (Blueprint $table) {
        $table->string('path');
    });
}

However, after executing php artisan migrate I get Nothing to migrate .

What am I doing wrong?

You'll need to rollback migration first which will destroy last migration tables with all data:

php artisan migrate:rollback

And then run php artisan migrate command again. It's the best option while app is in development.

If you don't want to do that for some reason (app is live etc), but just want to add a column then create a new migration and add this code:

public function up()
{
    Schema::table('units', function (Blueprint $table) {
        $table->string('path');
    });
}

And run these commands:

composer dumpauto
php artisan migrate

You'll have to crate a new migration for your column update. alternatively, you could roll back your last migration, add the missing column, and re-run migration (assuming you have not ran your migration on production)

to roll back: php artisan migrate:rollback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM