0
votes

I'm trying to add a new column to an existing table 'users'. I created a new migration 'add_privilege_column_to_users_table'. When I try to run the php artisan migration, I get an error that the 'users' table already exists. Am I missing something here?

I've already tried several methods from Googling and from other people that have had the same problem here at starckoverflow. But none of the code from here helped me.

class AddPrivilegesColumnToUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {

        $table->string('privilege_type')->after('remember_token');

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {

    });
}
}

PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")

That's the specific error that I get when I run php artisan migrate. I'm just trying to add a new column to that table.

Below is my migration status:

Run? | Migration
No     2014_10_12_000000_create_users_table
No     2014_10_12_100000_create_password_resets_table
No     2019_07_28_071643_add_privileges_column_to_user_table
1
Its because previously you would of had an error thus Laravel was unable to drop() the table. You need to go into your mysql and DROP TABLE users then re-run the php artisan migrate command ensuring you have ran migrate:rollback first - Jaquarh
Oh, but I failed to mentioned that I already have data in that table. If I do what you are suggesting, that will erase my data, correct? - Julio Sandoval
Show us your php artisan migrate:status. It seems you have rolled back one too many and Laravel thinks you dropped your users table at some point and is trying to rebuild it. - Jaquarh
I was able to figure it out. It turns out my tables have to be run. According to my migrate:status, it say 'No' under ran. Lol - Julio Sandoval

1 Answers

0
votes

It seems you rolled back too far, but your users table was not dropped thus Laravel, when trying to run migrations, is trying to re-create the table.

If you head over to your database, you'll be able to update the migration to say it has already executed thus skipping over that batch.

UPDATE migrations SET batch = 1 WHERE migration = '2014_10_12_000000_create_users_table';