2

Whenever I make new migrations and trying to migrate it the results I get is this. Even if I try to migrate:refresh and so on. What do you think the problem here? I have also checked my .env file. Thank you!

[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users ( id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at tim estamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

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

  • Possible duplicate of [Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already exists](https://stackoverflow.com/questions/46129270/laravel-5-5-error-base-table-or-view-already-exists-1050-table-users-already) – Nitesh Kumar Niranjan Sep 14 '17 at 01:13

2 Answers2

1

Use !Schema::hasTable('users'), it will check whether a table already exists in the database or not.

If you don't want to drop already exist tables then it is a good idea.

if (!Schema::hasTable('users')) {
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}
0

The problem is that you're trying to create a table that already exists in the database,

Either delete the users table from the database before running php artisan migrate:refresh

Or if you have made another migration to add a new column into the users table then you need to have

Schema::table('users', function (Blueprint $table)

Instead of

Schema::create('users', function (Blueprint $table)

It's important to not that Schema::create will create the table, and Schema::table is what you'd use when modifying an existing table.

n31l
  • 48
  • 1
  • 1
  • 9
  • I forgot to include that it is not the "users" table that I want to migrate in the database. But whenever I use php artisan migrate, It does not migrate the new table that I want to include in the database but instead it always give me same results like this one. – Noriaki Pacheco Apr 23 '17 at 13:26
  • if you run ``php artisan migrate:status`` you can look for the migration file with the (N) next to it that hasn't been migrated. In your case when the error is because the table already exists, you could either comment out the lines in the migration file, or if you are adding a new column make sure that you don't have ``Schema::create('tableName', function (Blueprint $table)`` twice. – n31l Apr 23 '17 at 15:21