Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run specific migration in laravel [duplicate]

Tags:

php

laravel

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?

like image 257
Keval Mangukiya Avatar asked Nov 07 '17 07:11

Keval Mangukiya


People also ask

How do I run a specific migration file in Laravel?

To run the specific migration in Laravel, you need to use --path option with the php artisan migrate command. Let's take a simple example, we have '2019_12_04_131405_create_payments_table. php' migration in the database/migrations directory and we would like to run this migration.

How do I refresh a particular migration in Laravel?

If you tried to run migration#2 without every having run #1 then it would fail. When you run artisan migrate then it will only run migrations that haven't already been applied unless you use migrate:refresh in which case it will reset and re-run all migrations.

How do I run a specific migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How do I run migration in Laravel again?

IF you want to re-migrate all the database, you can simply do: php artisan migrate:refresh . IF you want to make sure your database to be clean with your latest changes, you can drop your entire database tables and do php artisan migrate again. Also, you can try php artisan migrate --seed if you have any seeder.


1 Answers

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.

like image 172
Ravi Thummar Avatar answered Sep 21 '22 19:09

Ravi Thummar