Error Log
Three mistakes I made today and how I corrected them; posted for posterity.
Saturday, 2010-01-16 | Django, Programming
| "A woman may very well form a friendship with a man, but for this to endure, it must be assisted by a little physical antipathy." |
| Nietzsche |
I spent most of this weekend working on a top-secret django project.
As usual, I screwed a bunch of stuff up before finally brute-forcing my ideas into print and getting my applications and views working correctly. What follows are three things I learned about python, django and postgresql today:
Automatic thumbnail width/height resizing with PIL
If you're using the python image library's built-in thumbnail function to resize images, you might not know that you only need to specify the width or the height when resizing. Say, for instance, you've got some thumbnail-creating code that looks like this and creates thumbnails 30 pixels wide and 50 pixels high:im = Image.open("/path/to/image") im.thumbnail((30,50), Image.ANTIALIAS)
size = (30, "auto") im.thumbnail(size, Image.ANTIALIAS)
'duplicate key value' violations in django
If, while working on a django project, you run manage.py syncdb and get an error that looks like the following:psycopg2.IntegrityError: duplicate key value violates unique constraint "django_content_type_pkey"
/var/log/postgresqltoconnell@jane:~/$ sudo tail -f /var/log/postgresql/postgresql-8.3-main.log
[sudo] password for toconnell:
2010-01-16 09:21:39 EST ERROR: duplicate key value violates unique constraint "django_content_type_pkey"
2010-01-16 09:21:39 EST STATEMENT: INSERT INTO "django_content_type" ("name", "app_label", "model") VALUES (E'artist', E'disco', E'artist')
Basically, what happened here is that you ran django-admin startapp appname twice; i.e. you started an app of the same name twice and, when you went to add it to your application, with that syncdb command, you tripped a unique key constraint intended to prevent ambiguous calls to the database.
My Advice: drop the table and load the back-up--automatically created django database tables tend to dislike being tampered with.
Speaking of restoring postgres data...
When restoring data, remember to specify database name. If, for example, you do this:# su postgres -c 'psql -f demongin.sql'
A proper restore of a postgres dump looks like this:
# su postgres -c 'psql -f demongin.sql -d demongin'
