15

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

Naren Murali
  • 19,250
  • 3
  • 27
  • 54

22 Answers22

28

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');
    }
}
20

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.

Kaloyan Drenski
  • 950
  • 1
  • 13
  • 18
19

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

php artisan migrate:fresh

suman
  • 367
  • 2
  • 4
  • 2
    @Florin It will also create tables again – Rahul Rahatal Feb 18 '19 at 07:25
  • 5
    Take note that this will create everything from scratch, you will lose data. – Aadesh Dhakal Jun 09 '21 at 12:57
  • 2
    @Rajat: this is why one should never blindly use code found on this site or any other site, but instead should discover the ideas behind the code, and then create your own code based on knowledge gained. Otherwise, you use code "at your own risk". Best to place a *kind* comment mentioning your problem, down-voting the answer (if appropriate), and then move on. – Hovercraft Full Of Eels Jul 11 '21 at 21:43
8

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();

    });
}
Mohsen
  • 4,049
  • 1
  • 31
  • 31
6

Following command solved my problem:

 1. php artisan tinker
 2. Schema::drop('your_table_name')
 3. php artisan migrate
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
ZAFIR AHMAD
  • 79
  • 1
  • 3
3
if (!Schema::hasTable('users')) {
            Schema::create('users', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
        }

Use !Schema::hasTable('users') ,It will check whether a table already exist in database or not. If you don't want to drop table then it is a good idea.

2

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

  1. php artisan make:migration create_tableName_table --create=tableName.

  2. php artisan migrate.

  3. appear error,you can drop all file in migration and all table in database.

  4. create new table as 1.

  5. finish. okay.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
1

Sometime I get the same problem as yours and after I examine it's because sometimes I'm to lazy to type and copy paste the code from create_user_table.php

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

and I forgot to change the table name from this code

Schema::create('users',

To This

Schema::create('what_ever_you_want_to_name_it',

I hope this could help

Rankioto
  • 32
  • 3
1

Step 1:

php artisan migrate:reset

Step 2: Go to your database using PHPmyadmin (or similar) and delete all remaining tables - including the migration table.

Step 3:

php artisan migrate
Najathi
  • 2,529
  • 24
  • 23
0

I have different solution, i delete migration table, here my solution

<?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('migration');
    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');
}
}
  • This works for me: https://stackoverflow.com/questions/44141475/sqlstate42s01-base-table-or-view-already-exists-or-base-table-or-view-already – Anjan Biswas Jan 29 '19 at 07:07
0

just delete those columns first from database. and run composer update . and then finally run php artisan migrate it would solve your problem. the simplest solution for your problem.

Aniket
  • 982
  • 1
  • 11
  • 16
0

There are two possible solutions for this as mentioned on this link:

https://www.codespeaker.com/laravel-framework/solutions-for-common-errors-on-artisan-commands/

First is rollback

php artisan migrate:rollback

Second is dropping of tables.

smzapp
  • 809
  • 1
  • 12
  • 33
0

Solution:

  1. Go on database -> phpmyadmin if you are on localhost
  2. Delete everything on database that you created
  3. On cmd run php artisan migrate
besartm
  • 558
  • 1
  • 7
  • 14
0

Agree with Nitesh's answer but I think that's not the best practice to drop the table everytime for no reason. We could also use simple "if" check with condition Schema::hasTable('users') according to the docs. So we can use it as:

<?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()
    {
        if(!Schema::hasTable('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');
    }
}

This solution will also be feasible in the scenario when you will already have a table and need some modification then you will be able to do anything in the else statement as well.

Feroz Khan
  • 2,396
  • 5
  • 20
  • 37
  • I didn't know much then. I had started programming back then I was just a 13-year-old kid. and trying to answer questions I got banned from answering them. – Nitesh Kumar Niranjan Dec 12 '19 at 09:30
0

Migration errors usually occur when using php artisan migrate and your migration has an error.

Within Laravel, there are some ways to reverse something that has been performed, especially migrations to more robust databases like MySql, for example.

One way to be reversing the step taken

php artisan migrate

is to run the

php artisan migrate: rollback

It automatically reverses the last migrate performed You also have the possibility to delegate how many steps will be reversed with the step parameter.

The number says how many steps will be reversed

php artisan migrate: rollback --step=1
Lucas Coelho
  • 1,492
  • 1
  • 9
  • 14
0

Create your migrations by command php artisan make:migration create_category_table Then in your database/migrations set your necessary fields and databases,then run this command pho artisan migrate --pretend It will show sql statements,copy sql statement of category and paste it in database in sql and just click on run,your migrations would be there,no need to delete users table

0

Change your Mysql Engine Setting

In config/database.php, change your mysql engine setting to: 'engine' => 'innodb row_format=dynamic'.

Note: This is applicable to Laravel 5.2.14+

Drk
  • 425
  • 2
  • 12
0

you can reset everything by using this command

php artisan migrate:reset

and then you can run this command.

php artisan migrate 

remember reset will delete your users table. Please do not use manual approach to delete table or do anything with database. Laravel allows us to do everything by using commands and that's they beauty of it. If you want details about using Laravel migrations then please check this link.

https://laramatic.com/how-to-use-migrations-in-laravel-code-example/

0

Simple Solutions

Problem:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table Assign_Students already exists.

Here I had a problem with Assign_Students table you may have any

X Y Z Table
ANS: DROP YOUR X Y Z Table from your SQLDB

as I did from my MYSQL I DROP Assign_Students and then I created my New table Example:

php artisan make:model EmployeeSallaryLog -m

Now when you do:

php artisan migrate`

You will find both tables like:

1) Assign_Students
2) EmployeeSallaryLog
SternK
  • 11,649
  • 22
  • 32
  • 46
0

For Laravel 8.0, none of the earlier answers worked for me. Then I noticed the line below in the error details when running php artisan migrate:

Loading stored database schema: /Users/spedley/Sites/monkeys/database/schema/mysql-schema.dump

I deleted the mysql-schema.dump file, ran php artisan migrate again and it worked.

supernifty
  • 4,171
  • 2
  • 32
  • 24
0

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id bigint unsigned not null auto_increment primary key, name varchar(191) not null, email varchar(191) not null, email_verified_at timestamp null, password varchar(191) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

The code below works like magic. It runs nicely.

id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
P.Githinji
  • 1,459
  • 11
  • 5
0

To avoid such error, I think it's better to call Schema::dropIfExists('table-name'); before Schema::create(...) in your migration file. Then try to rollback your migration and migrate again your tables.

Codeparl
  • 300
  • 1
  • 8