okfnepal / election-nepal

A crowdsourced open portal that brings all kind of Data related to Nepal's Local Election.
http://electionnepal.org
MIT License
19 stars 11 forks source link

Can't load data from database fixture #37

Closed amitness closed 7 years ago

amitness commented 7 years ago

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

aatishnn commented 7 years ago

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.

amitness commented 7 years ago

@aatishnn Tried it. Still getting the same error.

aatishnn commented 7 years ago

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.

amitness commented 7 years ago

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

aatishnn commented 7 years ago

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.

amitness commented 7 years ago

Sure @aatishnn . Thanks for the help.

amitness commented 7 years ago

@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.

techgaun commented 7 years ago

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.

sagargg-zz commented 7 years ago

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

amitness commented 7 years ago

@sagargg Same error again.

aatishnn commented 7 years ago

@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
sagargg-zz commented 7 years ago

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

amitness commented 7 years ago

@sagargg Working now :+1: