Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

What is the typical process to add a field to a production database without loosing existing data

What is the typical process to add a field to a production database without loosing existing data?

Is migrate:rollback meant for this situation? If yes, how is this done?

Thanks

1 like
8 replies
zachleigh's avatar

You want to add anew column or table to an existing database? Or do you want to add a new value to an existing table? Whichever it is, you definitely dont want migrate:rollback. That undoes the last migration and whatever data was stored in those columns will be gone. Id say never use migrate:rollback in production.

1 like
fsdolphin's avatar

I want to add a new column to an existing table which is already in production and has data.

pmall's avatar

And with what value will you fill this column ?

1 like
fsdolphin's avatar

This is not a real project, I'm just practicing, but it would be an integer.

$table->integer('age');

Does the value metters?

willvincent's avatar
Level 54

For the sake of demonstration, lets say you're adding that age field to your users table...

php artisan make:migration add_age_field_to_users_table

Edit the resulting file, and fill it thusly:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class AddAgeFieldToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->integer('age')->unsigned()->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('age');
        });
    }
}

Run the migration: php artisan migrate

If you don't make the field nullable, or at least provide a default value, you might run into issues next time a user record is updated.. but if it's nullable, it can remain empty until it's populated by the user.. so that's probably the best bet in this case.

8 likes
pmall's avatar

Just make it nullable or with a default value.

fsdolphin's avatar

@willvincent In your example, does that means that we now have all of the default fields from the users table plus the one we added?

Default fields...

        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

...plus the one from the new migration

    $table->integer('age')->unsigned()->nullable();

Is it common to have multiple migrations for one table for situations like this where the client wants some additional information to be added to a certain table?

Thanks

willvincent's avatar

does that means that we now have all of the default fields from the users table plus the one we added

Yes, exactly.

Is it common to have multiple migrations for one table for situations like this where the client wants some additional information to be added to a certain table?

If those changes occur post launch, or late-ish in the development process, yes.

2 likes

Please or to participate in this conversation.