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

Ajab_khan's avatar

php artisan migrate command giving error

Hye I am learning Laravel 5.4 . all was going fine but now i have a problem when i run the php artisan migrate command it give me the below error , [ [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 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)

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

i have other migrations in my project and the artisan is not even running them and so no tables in the database too . please help me . i have also applied composer dump-autoload command

0 likes
10 replies
Ajab_khan's avatar

if I remove the user migration and run the command php artisan migrate:refresh it says this

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQ L: alter table password_resets add index password_resets_email_index(email))

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

tykus's avatar

It seems that your migrations table is out of sync with your schema; although your comment so no tables in the database too suggests that maybe you are connecting to a different database than the one you are looking at.

If you can tolerate losing all data in your existing tables, drop each table in turn and re-run migrations

Key too long error solution

Ajab_khan's avatar

i did so again and again but nothing . it will just write one table that is the first what ever it is and then nothing , but one thing is strange when i run

php artisan migrate:status command it show all the table when i see in the db it show only the tables that are already there

Ajab_khan's avatar

C:\xampp\htdocs\laravel>php artisan migrate:status +------+------------------------------------------------+ | Ran? | Migration | +------+------------------------------------------------+ | N | 2014_1012000000_create_users_table | | N | 2014_10_12_100000_create_password_resets_table | | N | 2017_1012094127_create_posts_table | | N | 2017_1012094713_create_comments_table | +------+------------------------------------------------+

but there are only two tables in the DB

tykus's avatar

The migrate:status command compares the migration files with the migrations table in your database, and tells you whether each migration has been run, so it is telling you that none of your migrations have run. This might be correct but depends on when you check the migrations status.

Back to the problem... if you are getting a table already exists failure, can you check that you are not trying to Schema::create('users', ... a second time. Perhaps you were trying to modify a table Schema::table('users', ... and used create by mistake????

ChristophHarms's avatar

Just speculating, but this is what I came across when I started to work with Laravel:

When Laravel runs migrations, it saves the status to a migrations table in the database. If a migration fails, it tries to run the rollback() method in the migration and sets the status to failed.

Now, if the rollback() method does for some reason not really rollback the changes made in the migration, Laravel thinks the migration has been rolled back, although it really hasn't.

So, with the next migrate command, it tries to run the migration again (because it's status in the DB is failed).

If the migration happens to create a table, and the rollback does not drop that table, this could lead to the behaviour you described, if the migration fails and the rollback does not actually roll back the change.

To fix this, first empty out the entire DB and run php artisan migrate again. Then go over your migrations and fix any rollback() methods. Make sure you can run php artisan migrate:reset(will rollback everything) without any errors, leaving you with an empty database.

RamjithAp's avatar

Step 1: Remove all your tables in database Step 2: Go to app/providers/appserviceprovider.php add this line 'use Illuminate\Support\Facades\Schema;' on top of the file and add this line inside boot function Schema::defaultStringLength(191); Step 3: Now try to execute migrate command

1 like
Ajab_khan's avatar

i rechecked every thing there is no error with my code and yeah i also fixed the key too long problem as described in the docs . ok here is one thing for you guys when i see into my migration table there is no info about any of my migration, i think it suppose to have info about migrations. and also i run the reset command it says nothing to rollback but if then i ran migrate command it says table already exists . what i know is that it is only trying to run the first migration in the list and no other one . when i removed that migration and run the migrate command it migrated again only one table which was the first one . so now can i have any solution >?????

tykus's avatar

Can you post each of the migrations here?

Ajab_khan's avatar

thank you all of you guys for the help the solution worked

step 1 : remove every thing from the DB with the DB too . step 2: create new DB step 3: app/providers/appserviceprovider.php (use Illuminate\Support\Facades\Schema;) use this at the top of the table. step 4: create new DB step 3: now run the command php aritsan migrate

the problem was actually caused by the key too long problem. when solved and created new DB worked fine. Thank you once again

1 like

Please or to participate in this conversation.