0

I am having a problem while running php artisan migrate on my second table after running my first users table in laravel. When I run the second table migration, I get this error:

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

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
  • For those who end up being here because of the "migrations" table is not created automatically, this [link](https://stackoverflow.com/a/60095493/10539212) might help. – Phantom1412 Feb 06 '20 at 13:01

4 Answers4

0

If you are starting off using Laravel Spark or you have used an authentication layer, then the "users" table will give you that error because it already exists. Look through your migrations and you should find a table named as such.

The other possibility is that you have not set up your environment variables correctly and you are using the same database as another project. If that previous project has a "users" table, then this error will also be shown.

parker_codes
  • 3,267
  • 1
  • 19
  • 27
0

Run this php artisan migrate:reset.

Next time when you want to add another table,you need to check first whether the table already exists or not. You can do this by using

if(!Schema::hasTable('users')){
    // Write your schema create code here
}

Save the file and than run php artisan migrate command.

Nikhil G
  • 2,096
  • 14
  • 18
0

php artisan migrate run all the migrations inside database/migrations folder. So, first you migrated the users table and then you created a new second table migration and ran this command so laravel tried to migrate the users migration again which already exists inside your database and this error is being thrown.

You need to run:

php artisan migrate:refresh

It'll will roll back all of your migrations and then execute the migrate command.

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
0

I have solved this Problem

Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already exists

by Changing My create_users_table.php

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}