I store all my app's data in seeds.rb locally. However, after I pushing everything to Heroku, the app works well, but without any data. I do not want to re-input the mass data again, so does anyone have any ways to help me?
If you push the app to heroku, you can seed the database with the following command.
heroku run rake db:seed
If you have changed migrations then first you need to do is run migration
heroku run rake db:migrate
then
heroku run rake db:seed
If you don't have any data in database the I would suggest following is, But caution it will remove all current data from heroku database.
heroku run rake db:setup
Hope this helps you
you have to be sure no migration is pending if no pending
just do
heroku run rake db:migrate
and it will word perfectly
I'll just add, because I haven't seen it yet.
heroku run rails db:seed
Will accomplish the same task.
The accepted answer of
heroku run rake db:seed
certainly used to work. However, as of May 2024 with Rails-7.0.8 and Ruby-3.1, it failed in my case and in a silent manner with no messages. It did not print out anything on the terminal (which my seeding script should even in a successful run), nor created any DB entries. Then I found
heroku run rails db:seed
worked as it should, like a charm!
Just don't forget DB migratons prior to seeding with:
heroku run rake db:migrate:status # to see the current migration status
heroku run rake db:migrate
Note
Interestingly, when I tried the rake
command when there were still pending DB migrations, it issued a warning message like
You have 2 pending migrations:
20240423111111 AddChannelToPosts
20240514111111 CreatePostAssocs
Run `bin/rails db:migrate` to update your database then try again.
So, the heroku ran rake db:seed
command must have worked to some degree. However, for some reason it did not complete its job… Possibly my seeding script contains something the (old) rake does not handle well? In my local environments, I haven't used rake at all since Rails 5, and exclusively use bin/rails
. It is good heroku accepts heroku run rails
.
© 2022 - 2024 — McMap. All rights reserved.