Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Specifications:

  • Laravel Version: 5.5.3
  • PHP Version: 7.1
  • Database Driver & Version: MariaDB 10.1.26

Description:

C:/Users/user/code/blog/>php artisan migrate

[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 aut
o_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 engine = InnoDB R
OW_FORMAT=DYNAMIC)

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

Steps To Reproduce:

 C:/Users/user/code/blog/>laravel new website

 C:/Users/user/code/blog/>php artisan make:migration create_lists_table --create=lists

 C:/Users/user/code/blog/>php artisan migrate

Problem

It Creates users table and give error but not creating lists table

like image 913
Nitesh Kumar Niranjan Avatar asked Sep 09 '17 09:09

Nitesh Kumar Niranjan


5 Answers

I Solved My Problem Myself 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');
    }
}
like image 166
Nitesh Kumar Niranjan Avatar answered Nov 13 '22 00:11

Nitesh Kumar Niranjan


Here are the steps I took to solve the same issue:

  1. In the console I wrote php artisan tinker

  2. Then, again in the console, Schema::drop('users')

  3. At the end php artisan migrate and it all worked.

like image 19
Kaloyan Drenski Avatar answered Nov 12 '22 23:11

Kaloyan Drenski


The simple way to solve this problem is run the following command

php artisan migrate:fresh

like image 18
suman Avatar answered Nov 13 '22 01:11

suman


You can skip migrate the table if table exist in database by add this code:

public function up()
{
    if(Schema::hasTable('users')) return;       //add this line to your database file

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();

    });
}
like image 8
Mohsen Avatar answered Nov 13 '22 00:11

Mohsen


Following command solved my problem:

 1. php artisan tinker
 2. Schema::drop('your_table_name')
 3. php artisan migrate
like image 6
ZAFIR AHMAD Avatar answered Nov 12 '22 23:11

ZAFIR AHMAD