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

successdav's avatar

App Deployed. yea! so happens if I add new column to user db

I am just wondering...

someday when I finally deploy an app successfully, and then I add an extra column to the user's table.

then run php artisan migrate:refresh. locally it updates my db table. but erased all table data...

when I redeploy this app online, what happens to my registered users?

will the table also erase everything online just to add the new column?

...Damn this question had got me thinking for hours... maybe now you can help me understand thank you

0 likes
7 replies
Sergiu17's avatar

Run just php artisan migrate. Without :refresh

takdw's avatar

As far as I know, well as far as my experience is concerned, you'll lose your data. I usually avoid altering existing tables after the app is deployed. If I have to add an additional field, here is what I do. I first do a full backup of the database just to be safe. Then I export only table I'm altering. After that I'll do the necessary modifications in the migration file. The key thing here is to make the newly added column nullable or define a default value for it. Then drop the table from the database (I also remove the table's row from the migrations table, not entirely sure if its necessary). After that I run php artisan migrate and the newly modified table will be migrated. But it will be empty, therefore I open up the table backup I made (just the table) and copy the part where is populates the table with data and I run it in the SQL interface I'm using which is PhpMyAdmin usually. This procedure works well, at least for me.

nemrut's avatar

your way is a long way, just make new migration for the existing table as example

php artisan make:migration add_age_to_users_table

edit your new migration as your need

Schema::table('users', function (Blueprint $table) {
    $table->integer('age')->nullable(); //nullable because by default existing users doesn't has any value for the age column
});

then simply migrate

php artisan migrate

your users table will have the new 'age' column and no data will be erased

successdav's avatar

@SERGIU17 - Thanks ... I have been thinking this all day, but now I know how it works. Once again, thanks.

1 like

Please or to participate in this conversation.