Django Test Complaining about Fixture
Asked Answered
A

2

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.

Alumna answered 16/10, 2013 at 23:12 Comment(2)
try --natural when you datadump so that you use natural keys and not db ids? docs.djangoproject.com/en/dev/ref/django-admin/…Truthvalue
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.Kiyokokiyoshi
K
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!

Killing answered 6/1, 2017 at 18:13 Comment(0)
L
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.

Lanctot answered 7/11, 2015 at 14:9 Comment(5)
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.Querist
@Herbert: Please start a new topic for this.Wiper
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.Querist
@Querist 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.Wiper
I edited your answer to highlight this uncertainty, thank you for your efforts!Querist

© 2022 - 2024 — McMap. All rights reserved.