2
votes

Here my migration table

    <?php

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

class CreateUserTable extends Migration
{

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->string('name');
            $table->string('email',50)->unique();
            $table->string('username');
            $table->string('password');
            $table->boolean('active');
            $table->rememberToken();
            $table->timestamps();

        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

when ever i try to migrate the following error occur

*[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre ady exists (SQL: create table users (id int unsigned not null auto_incr ement primary key, role_id int unsigned not null, name varchar(191) not null, email varchar(50) not null, username varchar(191) not null, pas sword varchar(191) not null, active tinyint(1) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp nu ll) default character set utf8mb4 collate utf8mb4_unicode_ci)

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

please tell me what should i do ?? i have already use migrate:reset or dumpautoload or rollback nothing happen . lots of time i edit or delete this usersfile and recreated it .

3

3 Answers

5
votes

Try like this

   <?php

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

class CreateUserTable extends Migration
{

    public function up()
    {
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->string('name');
            $table->string('email',50)->unique();
            $table->string('username');
            $table->string('password');
            $table->boolean('active');
            $table->rememberToken();
            $table->timestamps();

        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}
1
votes

this problem is showing when you want migrate tables that exist in database : for this problem just go to PHPMYADMIN manually and drop all table and now run command php artisan migrate

0
votes

DANGER - by dropping a table, rolling back or refreshing your database will destroy all data in that table or database and these methods are not recommended for production use and you should definitely not include drop table behavior in your migration.

It's clear there are a lot of "solutions" for this problem, but all the solutions I read through are very destructive solutions and none of them work for a production database. Yes, they get rid of the error, but at what expense?

Based on the number of solutions, it also seems that there could be several causes to this error.

My error was caused by a missing entry in my migrations table. Which required a very simple solution -- adding the entry back in. A simple issue like this definitely doesn't warrant a database refresh