About error PG :: UndefinedTable: ERROR: relation "XXXXXX" does not exist

Error details

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

Interpretation of error statement

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.

Cause

I made a mistake in the order in which the migration files were created. (Phenomenon in the case of heroku)

Commentary

Migrate on heroku (% heroku run rake db: migrate) It is done in the order of creation date.

"20200912095202_create_song_discs.rb" The first letter of the migration file is determined by the date it was created. (It seems that this part is the version.)

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.

Workaround

(1) Delete the migration file related to the intermediate table because the creation date of the intermediate table will be later.

(2) When the creation date of another table related to the intermediate table is earlier due to deletion, the intermediate table is created again.

③ Migration locally again% rails db: migrate

④ Complete the process up to deployment again! !!

Action flow (command)

Premise

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.

Check the order in which the migration files were created

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

Delete unnecessary migration files

Since I created an intermediate table called song_discs first this time Deleted to recreate the file.

Things to check before deleting the migration 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

Perform migration file deletion

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

Check if it has been deleted

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

Create a newly deleted migration

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 the created migration file

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.

Migrate to database

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.

Deploy to heroku again

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!

point

For heroku deployment Be careful about the order in which the migration files are created!

A word Memo

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

About error PG :: UndefinedTable: ERROR: relation "XXXXXX" does not exist
[Error] ActiveRecord :: NoDatabaseError FATAL: database does not exist
About the "Oops VFS connection does not exist" error and solution in AWS Cloud9
"Error: Package xxx does not exist" "Error: Symbol not found" when rewriting kotlin
eclipse package org.springframework.boot.web.servlet.support does not exist
operator does not exist: handling the error timestamp without time zone ~~ unknown
[React + TypeScript] Property'options' does not exist on type'HTMLElement'.