1

I have installed Laravel's auth and chatter forum package. When I tried to migrate the database, I got this error:

Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view alr eady 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, `email_verified_at` timestamp null, `password` va rchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb 4_unicode_ci')

  at C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databas e\Connection.php:664

    660|         // If an exception occurs when attempting to run a query, we'll  format the error
    661|         // message to include the bindings with SQL, which will make th is exception a
    662|         // lot more helpful to the developer instead of just the databa se's errors.
    663|         catch (Exception $e) {
    664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 T able 'users' already exists")
      C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databa se\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databa se\Connection.php:458

  Please use the argument -v to see more details.

I tried to migrate with the command:

php artisan migrate
1
  • Since Users already exists, find that file and comment out everything in the Up function. This will allow the migration to continue without deleting the file. Commented Jan 3, 2019 at 13:53

5 Answers 5

3

If you check at the error trace, it says almost at the bottom:

Base table or view already exists: 1050 T able 'users' already exists") C:\xampp\htdocs\Application\vendor\laravel\framework\src\Illuminate\Databa se\Connection.php:458

This means that the users table already exists so when you run your migrations it is trying to create a table that is already created in your database, hence the error.

So tho undo this migrations before running them again, you could do:

php artisan migrate:refresh

Check the documentations regarding rolling back migrations.

This will run the down() function of every migration file already migrated in your system before actually running the up() ones.

If you go to your users migration, you can see the down() function, it should look like this:

database/migrations/XXXX_XX_XX_XXXXXX_create_users_table.php

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

Whenever you create a migration, always implement the down() method, to make use of the rolling back options.

Sign up to request clarification or add additional context in comments.

4 Comments

Migrate:refresh shows same error. When I try to roll back migration shows "Nothing to roll back". Down function is looking like that.
If you go to your migrations table (the one that logs the migrations already run) it lists the migration of the users table? If not, you'll need to drop the users table manually (directly in your database)
Migrations table is empty. Tried to drop users table, but nothing!
If you already dropped the users table there shouldn't be any error (because there shouldn't be any conflict). Are you sure you are using the same dabatase connection that you are configuring?
1

A migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database:

php artisan migrate:refresh

// Refresh the database and run all database seeds...
php artisan migrate:refresh --seed

Another Solution is: Delete users table table from the database also delete users entries from migrations table.

After, execute run the migrate Artisan command: php artisan migrate

Comments

0

Well, always that I install laravel i get the same error. Try changing code in your migration files like this:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email',64{ADD THIS PARAMETER})->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

You have to add a second parameter to strings (max length) that are unique or keys, like email. Don't forget to clear your DB of the tables so when you run php artisan migrate the DB is clear.

Comments

0

i am sure this will work because i have the same issue but i fixed with this

 php artisan migrate:fresh

please try and check

UPDATE

Delete project and install new laravel project

and do chnages in your .env file and

in database.php

 'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => 'InnoDB',
    ],

replace your existiing code to this

and run

 php artisan make:auth 

 php artisan migrate

or

  php artisan migrate:fresh

i hope this time it will work

and make sure you have all the requirements install in your system https://laravel.com/docs/5.7/installation

Comments

0

This sometimes happens when there is already an instance of the table in the migration folder. When you try to install Laravel's auth and chatter forum packages it also creates a user table migration. To solve the problem please go to the migration folder and delete one of the user table migrations. After that, you can run php artisan migrate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.