11

Dumped data python manage.py dumpdata --format json --indent 4 --exclude auth.permission --exclude contenttypes > app/fixtures/app_test_data.json

Running python manage.py test app, I get the following error:

IntegrityError: Problem installing fixtures: The row in table 'django_admin_log' with       primary key '517' has an invalid foreign key: django_admin_log.content_type_id contains a value '28' that does not have a corresponding value in django_content_type.id.

Any ideas? I've had a lot of similar issues using dumpdata/Django's test-runner.

masonjarre
  • 821
  • 1
  • 8
  • 15
  • 2
    try --natural when you datadump so that you use natural keys and not db ids? https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---natural – mconlin Oct 17 '13 at 01:15
  • 1
    This is most likely occurring due to the order your fixtures are being created. Django is trying to create `'django_admin_log'` before `content_type` is created resulting in a broken relationship. You can try to reorder how the fixtures are being created to fix this. – kylieCatt Sep 07 '15 at 14:05

2 Answers2

2

If:

  1. You are just interested in recovering the information from a json you generated before knowing this issue
  2. You don't care about the data of administration logs
  3. You (or someone else) generated the json excluding auth.permission and contenttypes, using a command like: python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

You may recover the information trapped in the json with a text editor, opening the file and deleting the elements associate with the admin.logentry model.

That worked for me!

0

If you exclude the contenttype app as in your example, you may not be able to export any app that has foreginkeys against that app. Removing the --exclude contenttypes might make it work.

Herbert
  • 5,279
  • 5
  • 44
  • 69
Emil Stenström
  • 13,329
  • 8
  • 53
  • 75
  • I have the same problem as the OP and never used the exclude. If I execute `dumpdata` them, the content_type's are still not exported into the json (or yaml). That is `./manage.py dumpdata --format=json | python -m json.tool | grep content_type` does not output any content_type definition. – Herbert Apr 16 '16 at 07:43
  • @Herbert: Please start a new topic for this. – Emil Stenström Apr 16 '16 at 10:48
  • 1
    I'm sorry, my point was not to start a new topic, but to state that I doubt that the --exclude-option solves the OP, as no content_type's are dumped with or without the --exclude option. I think the solution is in using the `--natural-foreign` or `--natural-primary` option, but I'm not sure, so I wont post that as an answer. It seems to me that django magically imports stuff, like the content_type's for polymorphic behavior. they do not need to be explicitly included in fixtures. Id's might differ between this magic and the dump, --natural-... solves this somehow. But I don't know the details. – Herbert Apr 17 '16 at 21:30
  • @Herbert I'm not sure either. It just looks like a obvious problem to explicitly remove an app that is needed for another app to be dumped. But let's just leave my answer and your comment here and maybe it helps someone sometime in the future. – Emil Stenström Apr 18 '16 at 15:59
  • I edited your answer to highlight this uncertainty, thank you for your efforts! – Herbert Apr 19 '16 at 08:29