Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaster's avatar

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

I am getting the below mentioned error when i run

php artisan migrate

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

create_jobs_table.php


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

class CreateJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('jobname', 40);
            $table->timestamps();
        });
    }

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

create_job_uploads_table.php

<?php

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

class CreateJobUploadsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('job_uploads', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('job_id')->unsigned();
            $table->foreign('job_id')->references('id')->on('jobs')->onDelete('cascade');
            $table->string('upload');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('job_uploads');
    }
}
0 likes
34 replies
bashy's avatar
bashy
Best Answer
Level 65

If it already exists before you did artisan migrate it will of course say this. Did you do migrate and it broke half way? You can reset it or just delete the table and try again (if you are in local dev and can delete it).

php artisan migrate:reset 
20 likes
developershiva94's avatar
   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();
        });
  } 
      above see this condition and save this and run php artisan migrate
 command and your table show in db
13 likes
Mwangaben's avatar

You have made my Sunday morning bro. with the that if check thing

1 like
hishamhadraoui's avatar

well it just happend to me and i deleted the tables manually then i did migration all over again after that it worked .

1 like
murph133's avatar

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
12 likes
sanjitsingh's avatar

If you are using Laravel 5.5 use migrate:fresh command

php artisan migrate:fresh

It will drop all the existing tables without rolling back or calling down method of migrations.

Then you can migrate all you migrations again with

php artisan migrate

Correction: Fresh migrates itself. No need to use migrate command after. Thanks @bashy for correcting me :)

13 likes
aravvi's avatar

run php artisan migrate:fresh with precautions. Because it will drop all of your existing tables regarding the prefixes. Which means if you have table in the same database related to other functions or project that also will be removed.

For more info please check Laravel Documentation

1 like
bashy's avatar

@sanjitsingh Actually, the fresh command migrates itself! No need to run migrate :)

$ artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
4 likes
eeemizan's avatar

What the fucking thing you are taking , Why i will also drop my data from database? I will intact my existing table data and also migrate simultaneously.

1 like
bashy's avatar

@eeemizan Talking to me? You can seed your data with --seed added onto that command.

dannamite's avatar

I tried all the migrate command options but nothing worked except migrate:fresh as suggested by @sanjitsingh. Thanks.

1 like
eliasfj's avatar

I do not understand, I had the same problem and I tried php migrate:fresh and it works, but men... it delete the info I had. I tried to add the --seed command but it still deleted the info. Is there any way to migrate without removing info? @bashy

vschavala's avatar

@eliasfj just delete the user migration schema in migration folder and try it. it will work

Jakir-lv's avatar

I am getting this message, when press "php artisan migrate" command>> In Connection.php line 664:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'password_resets' already exists (SQL: create table password_resets (email varchar(255) not null, token varchar(255) not null, created_at timestamp null) defau lt character set utf8mb4 collate utf8mb4_unicode_ci)

In Connection.php line 458:

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

how to fix it please help me

Jakir-lv's avatar

I am getting this message, when i use "php artisan migrate" How to fix this problem? SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (S QL: alter table password_resets add index password_resets_email_index(email))

MZev's avatar

I found the solution [here] (https://laracasts.com/discuss/channels/general-discussion/syntax-error-or-access-violation-1071-specified-key-was-too-long)

The problem appears to be that columns that are using the ->unique() constraint can't have more than 1000 characters(?).

I changed

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

to

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

And all was fixed. Be sure to make the appropriate changes to all migrations. I changed it in create_users_table, create_password_resets_table and a couple of my own tables.

rrpathi's avatar

Modify Some Changes in Your app\Providers\AppServiceProvider.php use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }

palak's avatar

I had a same issue. For me also "php artisan migrate:fresh" command worked. I am using Laravel 5.6 version. Thanks for the solution.

BadihBarakat's avatar

Well, for me, it appeared to be the problem of the unique key's. Once solved, everything went fine, including using migrate and migrate:refesh after adding a new migration. No data was deleted when using php artisan migrate. [EDIT:] I have to state that php artisan migrate will add any new migration settings not added before and will not implement changes to existing ones. When I tried to implement changes to existing migrations, it did delete the records from the table, so YES, data IS going to be lost if we will use php artisan migrate:refresh / migrate:reset. !!!

immeekk's avatar

I just added the code below into 'AppServiceProvider.php' and ran php artisan migrate:fresh, it worked. (Laravel 6).

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

The file is located at: app\Providers\AppServiceProvider.php

jove's avatar

@immeekk this has nothing to do with the issue, it's also 3 years old, and solved.

devianvisuals's avatar

If you want to get a clean "php artisan migrate" without this "Base table or view already exists" error and without deleting/reseting the other database tables with data. You should delete the migration data inside the "migration" database table after deleting a table from your database

Kingisdave's avatar

First step: Check your category and post migration file Check your migration files and check the public function up and public function down if the tables_names are correct and then check on your http://localhost/phpmyadmin/index.php and then go to your database

Second step: Check migrations table Inside your database, check for the 'migrations' table, It looks like this Picture of some rows inside a migrations table, you will find out that those two tables(post and category) are not inside

Third Step: Copy Names of Migrations Files Go into your laravel-app/database/migrations folder or IDE to that folder named migrations, then copy the name of both migrations files

Fourth and Final Step: Create new Migration rows and paste the copied names Back to your localhost database migrations table, create new rows for the two copied names and paste the names accordingly numerically

AND THEN RERUN THE MIGRATION AGAIN. IT IS DONE php artisan migrate

Please or to participate in this conversation.