1
votes

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
4
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.aynber

4 Answers

2
votes

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.

0
votes

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.

0
votes

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

0
votes

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