For Deploy to heroku, run "% git push heroku master" and Error when executing the following command
Terminal
% heroku run rake db:migrate
The error after executing the above command is
Terminal (part of error statement)
PG::UndefinedTable: ERROR: relation "XXXXXX" does not exist
#"XXXXXX"Table name
Omitted below
Because the intermediate table name was included in the error statement It turns out that it is a table-related error.
Because it was working fine locally I think this time there are errors that can only be encountered in the production environment.
I made a mistake in the order in which the migration files were created. (Phenomenon in the case of heroku)
Migrate on heroku (% heroku run rake db: migrate) It is done in the order of creation date.
This time, first create a file related to the intermediate table (song_discs table), Later I created a file about the table (discs table) about that table. It will be an error caused by that. In short, I think the intermediate table can be created later.
It has been described in the migration file of the intermediate table. Since I work by touching the file, I copy (back up) it to another location so that I don't have to worry if the contents are lost. ❗️ Please be sure to read ❗️ As a process to change the file creation order, Delete the file! The contents will also disappear! "I want to use the description in the file as it is!" "I don't want to change the content!" At this point, please copy the description in the file (for backup) somewhere. Please note that the contents will be destroyed when the file is deleted.
You can check the history with this command
Terminal
% bundle exec rake db:migrate:status
Check the Migration ID to find out the version They are arranged in the order in which they were created.
Example
database:app name_development
Status Migration ID Migration Name
--------------------------------------------------
up 20200723033017 Devise create users
up 20200727050028 Create songs
up 20200731024511 Create user songs
up 20200801063906 Create songcolors
down 20200821150924 Create song discs ← The intermediate table is created first.
up 20200821160923 Create discs
Since I created an intermediate table called song_discs first this time Deleted to recreate the file.
The file Status you want to delete must be "down".
Example
database:app name_development
Status Migration ID Migration Name
--------------------------------------------------
up 20200723033017 Devise create users
up 20200727050028 Create songs
up 20200731024511 Create user songs
up 20200801063906 Create songcolors
down 20200821150924 Create song discs
up 20200821160923 Create discs
If it is "down" like the above example, proceed to the deletion work as it is If it is "up", change it to "down" and then delete it. ("VERSION = migration ID of the file you want to delete" can be deleted)
Example command to bring down Status
% rails db:migrate:down VERSION=20200821150924
It may be more reliable to copy the name of the migration file as it is.
Terminal
% rm -rf db/migrate/File name you want to delete
Example
% rm -rf db/migrate/20200821150924_create_song_discs.rb
Again "% bundle exec rake db: migrate: status" If it disappears, the deletion is successful
Example
database:app name_development
Status Migration ID Migration Name
--------------------------------------------------
up 20200723033017 Devise create users
up 20200727050028 Create songs
up 20200731024511 Create user songs
up 20200801063906 Create songcolors
up 20200821160923 Create discs
Execute the command to create only the migration file.
Terminal
%rails g migration The name of the migration file you want to create
Example
% rails g migration create_song_discs
The completed file example is "20200912095202_create_song_discs.rb" Be careful not to forget to insert create!
It is safe to check the history with "% bundle exec rake db: migrate: status" here.
Edit if necessary before database migration (It is the content described on the premise.)
In my case this time, the contents are the same as in the deleted file, so I pasted what I copied and saved in advance.
Since the Status cannot remain "down", execute the following command Change to "up".
% rails db:migrate
Also, I'll check here to see if it works in my local environment! Since I am touching the file, I will check if something is wrong and proceed to the next process.
If you come to this point, the countermeasures have been completed, so Try deploying! !!
How do you do it? If you have any questions, please click here. (https://qiita.com/kusaharajiji/items/9287235d56843c03734c)!
Thank you for your hard work! !! Did you deploy it? I'm very happy to resolve the error!
For heroku deployment Be careful about the order in which the migration files are created!
I learned a lot this time. If you make it casually, you will just stumble as if you were saying, "You don't understand here!" Thank you for the error. As I was writing the article, I found a new lack of understanding.
① About "up" and "down" that appear in the history of migration files (2) Regarding the rules for creating migration files (is it limited to heroku?)
I'm curious about this too, so I'd like to write it again, so I'll record it here.
Also, come up with other solutions, When creating a migration file, I tried to delete it by reversing the command to create a model (% rails g model model name) and try again, but it seemed to be difficult because there were files deleted more than necessary, so I stopped. It was! You have to know what the delete command deletes, right? (Of course ... lol)
Please read to the end Thank you very much! See you again: relaxed :: wave:
Recommended Posts