Sunday, March 1, 2015

6.9 Exercises - How to Resolve an IntegrityError

One of the exercises in 6.9 is:

Update your population script so that the Python category has 128 views and 64 likes, the Django category has 64 views and 32 likes, and the Other Frameworks category has 32 views and 16 likes.

I kept getting a long error that ended with:
django.db.utils.IntegrityError: column name is not unique

This response on StackOverflow helped a lot. The key is that in models.py, the Category name is set to unique=True, so since a category with name Python already existed in the database (and thus was not unique), we were not able to update it with views and likes. 

After migrating the changes to models.py, and before running populate_rango.py, go into the admin page, delete the three categories. Alternatively this thread taught me that I could also delete the db.sqlite3 file and then run this command in terminal: python manage.py syncdb

Then create the superuser again, apply migrations and run populate_rango.py. You should now see views and likes updated in each category. Success! (This also comes in handy in the next chapter, when you create slugs).

No comments:

Post a Comment