May Sale! All accounts are 40% off this week.

simpel's avatar

How to run a migration?

Hello!

I must be getting something wrong here regarding migrations...

I've created migration file for a table, run it once and all is fine. My problem starts when I want to make a change in the migration file/database table. This is my workflow:

  • Make a change in the migration file, for example remove a column in the schema
  • SSH into my Homestead server
  • cd to my project folder and runt 'php artisan migrate'

Then nothing happens, the command just returns "Nothing to migrate.". Why isn't it detecting the change I did in one of my migration files?

0 likes
7 replies
phpMick's avatar

That migration has already been run, you need to add a new one to amend the table.

If you want it to modify it and run it again, you need to do:

php artisan migrate:refresh

But, you will lose your data.

Are you seeding your data? (I recommend that you do.)

If you are you can do this:

php artisan migrate:refresh --seed

Mick

2 likes
tisuchi's avatar

You may try this-

  • in migrations table, delete the last / the respective tables record that's column you have changed.

Now run php artisan migrate and hope you will get the changes.

Of course, you will loose your data for that table in this case.

simpel's avatar

Hi! Thanks for the replies!

@phpMick, how do I amend my change? What is seeding?

phpMick's avatar

You make the change by amending the original migration file. php artisan migrate:refresh will dump the table and create it again.

I prefer to work like this, so you don't end up with a load of migrations.

Seeding is putting the data in your db.

simpel's avatar

@phpMick Ok, but then that is what I am doing, amending the changes. That is I change for example the schema in the migration file for the table. But somehow 'php artisan migrate' doesn't seem to recognise the change. Do I need to change the filename of the migration file or something like that?

phpMick's avatar

When a migration has been run, a record is created (in the migrations table), so that this migration does not get run again.

If you want the same migration to get executed again:

php artisan migrate:refresh

"The migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database:"

I would read all of this again:

https://laravel.com/docs/5.4/migrations

Please or to participate in this conversation.