skip to Main Content

Specifications:

Laravel Version: 5.4
PHP Version: 7.0.9
Composer version 1.9.0
XAMP

Description:

In Connection.php line 647:

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

In Connection.php line 449:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table ‘users’ already exists

Problem:
I have created models and tables of user and Product. It successfully created both migrations and tables but failed to migrate on phpmyadmin sql.

Steps I tried:
I have dropped all database and recreated it but still it gives error.
I have also used tinker but error is same.

code:

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        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');
    }
}

3

Answers


  1. Try to add the following condition before Schema::create()

    if (!Schema::hasTable('users')) {
            Schema::create('users',...)
    }
    
    Login or Signup to reply.
  2. In your users table migration file add this line in up() method

        Schema::dropIfExists('users');
    

    Like this

        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();
         });
        }
    
    Login or Signup to reply.
  3. Check your migration table if the “User” Table is recorded there, delete it and then do a single migration using this artisan command

    php artisan migrate:refresh --path=/database/migrations/fileName.php
    

    Or Do reset the migration using the following and then migrate again

    php artisan migrate:reset 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search