How can I run specific migration in laravel [duplicate]
Asked Answered
M

1

216

I create on address table migration but one migration is already in the database it gives following error :

Base table or view already exists: 1050 Table 'notification' already exists

So, Can I run specific migration? How can I run in Laravel?

Mammalian answered 7/11, 2017 at 7:8 Comment(4)
In your case, I would drop the schema/database and create it again.Boudoir
Could a mod please remove the dupe warning? A Laravel 4.x answer is far too outdated to consider this question answered. @communityCascade
Exactly - I wanted to post an answer, but it is Laravel 5 specific, while the "duplicate" question explicitly states Laravel 4.Mail
Commenting for better help to the new comers: Visit this tutorial for better understanding: scratchcode.io/laravel-run-specific-migrationOttar
R
509

TLDR;

"By the book":

If there are already migrated tables and there is some data stored in those tables, be careful with php artisan migrate:refresh. You will lose all your data!

For this specific question OP has already run the migration and by the book if he wants to run the same migration again, then first he should rollback with php artisan migrate:rollback. This will undo the last migration/s.

Then you can run php artisan migrate and all NOT migrated migrations will be migrated.


If you created more migrations and they are not migrated yet, to run only a specific migration use this:

php artisan migrate --path=/database/migrations/full_migration_file_name_migration.php

And sometimes if there is something messed up and you get errors on migrate, saying that the table already exists you can manually delete that specific entry from migrations AND the table which causes the problem in your DB and run php artisan:migrate to recreate the table.

Robena answered 7/11, 2017 at 7:15 Comment(20)
Add the .php extension in path like php artisan migrate --path=/database/migrations/my_migrations.phpEquipage
Showed Nothing to migrate. to me. Why? Does the migration need to be already on the migrations tables? I tried with one already there too and it gave the same thing. Is this path my full path to the file or is it relative to my app's root? Edit: also tried with the full path and it says the same thing.Grissel
Specifying --path=path/to/specific_migration didn't work for me, I had to adopt the other strategy of moving the specific files into a subfolder, then run --path=path/to/subfolder, it is a big shortcoming. Watch out though: migrate:reset will ignore --path !Edmondedmonda
Shows 'Nothing to migrate'.Lasseter
I think this only works with later Laravel version (5.X). It worked for meProtomorphic
Moving file to subfolder was the solution for me.Asben
this will work if the migration already not in migrations table. if migrations table contain this migration then remove and run command againTourniquet
If, you want to delete and create the table, again use: ` php artisan migrate:refresh --path=/database/migrations/file_name.php`Katykatya
Another option is to remove the migration that ran from the migrations table, then run php artisan migrate and it'll run that one you just removed.Gethsemane
Instead of only migrate use migrate:refresh, that'll work. For example, it will be php artisan migrate:refresh --path=/database/migrations/my_migrations.phpTirade
It did not work for me when I created the migration file manually. It worked when I did it via php artisan make:migration migration-name-hereUnmanned
you just need to execute "php artisan migrate" , Laravel knows which files already been executed and only run the latestTramroad
If you are using gitbash, it will not work. Try with cmd.Facies
migrate:refresh is BAD. Laravel will DROP ALL TABLES at first.Reactance
Don't use refresh because it creates the risk of overriding all the existing data. The marked answer is incorrect.Discontinuity
If it says file not found add a dot . immediately after --path= i.e. php artisan migrate --path=./database/migrations/full_migration_file_name.phpSpontaneous
BEWARE OF USING "migrate:refresh" in production, will drop all of your data!Rockie
If it says Nothing to migrate, check out your migrations table and delete any entry about the migration you're trying to run.Irretrievable
worked for laravel 6.xLex
worked for me after removing the / from start of path php artisan migrate --path=database/migrations/full_migration_file_name_migration.php > Laravel 6.2Stationery

© 2022 - 2024 — McMap. All rights reserved.