knirirr
knirirr

Reputation: 2807

Restoring Django dump

I've been running a daily dump of a production Django application as follows:

 ./manage.py dumpdata  --exclude=contenttypes --exclude=auth.Permission -e sessions -e admin --all > data.json

Normally, restoring this to another installation for development hasn't caused a problem, but recently attempts to restore the data have caused this:

 ./manage.py loaddata -i data.json
 django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'reversion_version' with primary key '1' has an invalid foreign key: reversion_version.content_type_id contains a value '14' that does not have a corresponding value in django_content_type.id.

This suggests to me that the problem has been caused by the recent addition of django-reversion to the codebase, but I am not sure why and I have not been able to find any means of importing the backup. Some posts suggest that using natural keys may work, but then I get errors like:

django.core.serializers.base.DeserializationError: Problem installing fixture 'data.json': [u"'maintainer' value must be an integer."]

"maintainer" is in this case a reference to this bit of code in a model definition in models.py:

maintainer = models.ForeignKey(Organization,related_name="maintainer",blank=True,null=True)

Does anyone has any suggestions as to how I might get this dump installed, or modify the dump procedure to make a reproducible dump?

I note that the production site is using Postgres and the test site has SQLite, but this has never been a problem before.

Upvotes: 1

Views: 1612

Answers (2)

knirirr
knirirr

Reputation: 2807

That was rather painful. It seems that the way to fix it was to dump django_content_types as csv from the production posgres database, delete the IDs from the resulting csv file, then do the following on the SQLite database for the test version:

 CREATE TABLE temp_table(a, b, c)
 .mode csv
 .import content_type.csv temp_table
 DELETE FROM sqlite_sequence WHERE name = 'django_content_type'
 DELETE FROM django_content_type
 INSERT INTO django_content_type(name,app_label,model) SELECT * FROM temp_table

That had the effect of setting the ids of the entries in the django_content_type table to match those in the dump, allowing the restore to proceed.

Upvotes: 0

allcaps
allcaps

Reputation: 11228

On your local machine clone your project and do something like this:

  1. Checkout the project at state that was used to create the dump.
  2. Create a new database and tables.
  3. Load the dump.
  4. Update the code to current state.
  5. Run migrations.

Upvotes: 1

Related Questions