okfnepal/election-nepal

Can't load data from database fixture

Closed this issue ยท 13 comments

Running this command is giving me error.

python manage.py loaddata  election-nepal-fixture.json 
  File "/home/anon/Desktop/workspace/election-project/election-nepal/venv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: Problem installing fixture '/home/anon/Desktop/workspace/election-project/election-nepal/election-nepal-fixture.json': Could not load contenttypes.ContentType(pk=1): UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

@techgaun

It should work if you use loaddata on a clean database on which migrations have just been applied. Try deleting "dev.db", doing ./manage.py migrate and run the command again.

@aatishnn Tried it. Still getting the same error.

Can you try with this file? I created this dump by loading the previous and re-dumping with contenttypes and auth.permission excluded.
fixture.json.txt

Rename to .json before importing.

It's strange why it didn't work.

@aatishnn This worked. Should we replace the current with this?

fixture.json.txt

This file has been generated with:

./manage.py dumpdata --natural-foreign -e contenttypes -e auth.Permission -e sessions -e conf.setting > fixture.json

Yes, please update the repo with this file. This also excludes sessions.

Sure @aatishnn . Thanks for the help.

@aatishnn Yo dump ma naya updated links xaina raixa. Dataset haru load vairaxaina coz uta GitHub repo ma link change vaxa data ko.

Sagar bro @sagargg, can you please create latest dump using command Aatish dai mentioned above.

A way to get around the stale db fixture (& have it kinda automated) would be to use a pre-commit hook. Something like below (untested but should generally work with bash shell) can be copied to .git/hooks/pre-commit file in the project root dir (maybe @sagargg or someone who is the one playing most with db does this):

#!/bin/bash

echo "rebuilding db fixture as pre-commit hook!!!"
python manage.py dumpdata | python -m json.tool - election-nepal-fixture.json
exit_code=$?
if [[ "${exit_code}" == "0" ]]; then
  git add election-nepal-fixture.json
else
  echo "Rebuilding db fixture failed :("
  exit 1
fi

The executable permission should be given via chmod 4 .git/hooks/pre-commit. This hook automatically re-runs a db fixture dump, prettifies using one of the python modules and then if the task was successful, adds the fixture.json to the staged changes before commit. One catch is the hook should have venv activated in the shell.

@Studenton i just updated the latest db fixture. if you getting trouble with that let me know.

@sagargg Same error again.

@sagargg you need to create a dump using the command I mentioned earlier. The dump you created looks like a dump from the live app which included sessions and salted passwords.

You might want to delete that file using https://help.github.com/articles/removing-sensitive-data-from-a-repository/

And then, create a new fixture using the command below:

./manage.py dumpdata --natural-foreign -e contenttypes -e auth.Permission -e sessions -e conf.setting> fixture.json

@Studenton try it again with updated file. I dumped this file as told by @aatishnn

@sagargg Working now ๐Ÿ‘