Search code examples
postgresql

restoring database generates errors that have no apparent explanation


an import for testing puprposes on a local machine via pg_restore -c -d db_name -Fc backup_file is returning the following.

pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 3398; 2606 29392 FK CONSTRAINT chains fk_rails_998a00e446 deploy
pg_restore: error: could not execute query: ERROR:  constraint "fk_rails_998a00e446" of relation "chains" does not exist
Command was: ALTER TABLE ONLY public.chains DROP CONSTRAINT fk_rails_998a00e446;
pg_restore: from TOC entry 3435; 2606 29222 FK CONSTRAINT shops fk_rails_24e1b904b4 deploy
pg_restore: error: could not execute query: ERROR:  constraint "fk_rails_24e1b904b4" of relation "shops" does not exist
Command was: ALTER TABLE ONLY public.shops DROP CONSTRAINT fk_rails_24e1b904b4;
pg_restore: from TOC entry 3188; 1259 29056 INDEX index_chains_on_site_id deploy
pg_restore: error: could not execute query: ERROR:  index "index_chains_on_site_id" does not exist
Command was: DROP INDEX public.index_chains_on_site_id;
pg_restore: warning: errors ignored on restore: 3

the migration files from rails for both classes do have:
t.bigint :site_id, index: true, default: 1
and checking the database does show that both classes have the indicies mentioned in the warnings

Foreign-key constraints:
    "fk_rails_998a00e446" FOREIGN KEY (site_id) REFERENCES sites(id)

Foreign-key constraints:
    "fk_rails_24e1b904b4" FOREIGN KEY (site_id) REFERENCES sites(id)

Thus the warning messages are unclear in terms of how they were generated, as well as their relative consequence.


Solution

  • -c means pg_restore tries to drop all the objects it is intending to create, including indexes and foreign key constraints. If that object doesn't actually exist at the time in the restored-to server, then dropping it leads to an error. But then later on in the same pg_retore run, those object are successfully created so by the time it finishes they do exist. If you check after the pg_restore, they will exist because they were created. If you check before, they didn't exist.

    I don't know what you or rails are trying to do, or why you are specifying -c in the first place, but presumably these are just nuisance messages for you.