1

when I run the command php artisan migrate it results in the following error

[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 timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

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

I am using Laravel 5.4 on Windows.

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
T_wz
  • 11
  • 5
  • Possible duplicate: http://stackoverflow.com/questions/26077458/laravel-migration-table-already-exists-but-i-want-to-add-new-not-the-older – Abdul Rafay Mar 08 '17 at 16:52

6 Answers6

1

It's letting you know that the 'Users' table exists already - when you're running your migration - it's trying to create that (but it already exists)

This is generally because you've tried running the command 'php artisan migrate' before. This has to be rolled back to have those changes 'undone', or have the DB tables cleared out.

You can run:

php artisan migrate:rollback 

That should get rid of all the tables - then you can run

php artisan migrate

and it should load everything appropriately.

The alternative approach? Log into your DB and physically drop those tables from the DB. Then you can run the migrate again and it will work.

A little gotcha: Check your migration first though: it should have this function:

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

If it doesn't, the rollback won't drop the users table and you'll still have this issue (which means you'll have to log into the DB and drop the tables manually).

If you create the migration with this command, the rollback functionality will be included automatically:

php artisan make:migration create_yourtablename_table
Hanny
  • 2,078
  • 6
  • 24
  • 52
0

If you pasted the exact code in that question then I can see a simple spelling error

email varchar(255) not n ull,

I know its silly answer given by me. but you can use:

php artisan migrate --force
php artisan migrate:refresh
php artisan migrate:reset

And better use this. Probably it will work:

php artisan migrate:rollback
S M Iftakhairul
  • 1,120
  • 2
  • 19
  • 42
0

Follow These steps to migrate

Check you database if there is 'users' table exits then DROPuserstable

Now

Step 1. To create a migration, use the make:migration command :- php artisan make:migration create_yourtablename_table

Step 2. Type this command : php artisan migrate

Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
0

Use

php artisan migrate:rollback
0

I had the same problem , what i did was comment out lines in (up function) in users and password_reset migration and then i tried it worked. I dont know whether it is right approach ! correct me if i am wrong

designerdarpan
  • 197
  • 1
  • 3
  • 19
0

Answer from https://github.com/laravel/framework/issues/21100.

Your Problem can be Solved by Changing 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()
    {
        //Add this line
        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');
    }
}