django-dbbackup: Error: constraint does not exist when restoring a backup on a fresh db

  1. Backed up the database
  2. Restored the database from the backup

=> Works fine ✅

  1. Back up the database
  2. Wipe out the database
  3. Do the initial django migrate
  4. Restore the same backup as in the previous case

=> I get the following error ❌

ERROR:  constraint "socialaccount_socialapp_site_site_id_2579dee5_fk_django_site_id" of relation "socialaccount_socialapp_sites" does not exist

I guess the issue comes from constraint name not being constant (2579dee5 par above).

Why not use IF NOT EXIST (https://www.postgresql.org/docs/9.0/static/sql-altertable.html) ? Or simply wipe out the whole database before importing and remove all the DROP commands?

I guess I miss some historical data (reasons behind decisions), but for now I have a hard time figuring how to restore this dump without manual edit of the relevant lines.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 4
  • Comments: 33 (7 by maintainers)

Most upvoted comments

All settings specified above doesn’t work for my installation, so I suppose the built-in dbrestore doesn’t work at all for postgres. (Django==1.11.9, django-dbbackup==3.2.0)

But I was able to restore database by psql directly, actually it’s not so hard.

su postgres
psql -d dbname -f ./backup-file.psql

Media restore works ok, so I’m satisfied 😃

still relevant after 3 years…

I solved my issue by removing all the DROP lines, and replacing DROP SCHEMA public by

DROP SCHEMA IF EXISTS public CASCADE

but being able to load dumps in this case would be super cool 😄

Solved this by adding to settings.py:

DBBACKUP_CONNECTORS = {
    'default': {
        'RESTORE_SUFFIX': '--if-exists',
    },
}

@ZuluPro I’m kind of confused here, how do I use the new connector? Doesn’t the pip install take everything needed from your branch?

For now, the manual steps I take to restore a dump from another host:

  1. Remove all the lines that drop constraints, indexes, tables.
  2. Just keep the DROP SCHEMA line and append CASCADE at the end.

That’s all folks.

Still relevant for Django 3.2 and Python 3.10

Unfortunetly 'dbbackup.db.postgresql.PgDumpBinaryConnector' did not make any difference.

The problem with ImportError: No module named postgres Is due to a typo in the connector settings…

'CONNECTOR': 'dbbackup.db.postgres.PgDumpBinaryConnector', should be: 'CONNECTOR': 'dbbackup.db.postgresql.PgDumpBinaryConnector',

@mikeschaekermann

@thimma11 you should just install dbbackup==3.2.0 and then set the connector settings as above, then dbbackup/dbrestore should work.