Posted by & filed under QGIS.

Sometimes I make changes to my django models that causes syncdb to not run and I can’t figure out why. Here is an example of an error I might get:

python syncdb
. ommitted for brevity
    return self.get_query_set().get_or_create(**kwargs)
  File "/usr/lib/pymodules/python2.6/django/db/models/", 
    line 343, in get_or_create
    raise e
psycopg2.IntegrityError: duplicate key value violates unique 
    constraint "auth_permission_pkey"

These messages are a bit opaque so one useful technique I found is to temporarily enable postgresql logging to a file:

sudo vim /etc/postgresql/8.3/main/postgresql.conf

Now add or uncomment the following:

log_destination = 'stderr'
logging_collector = on
log_directory = '/tmp'
log_statement = all

Next you need to restart PostgreSQL (obviously you dont want to do this on a production system without taking the needed precautions!).

sudo /etc/init.d/postgresql-8.3 restart

Next I ran my syncdb command again and at the same time watched my PostgreSQL logs being generated in /tmp (I used the unix tail command to do that):

2010-03-29 14:29:22 SAST LOG:  statement: INSERT INTO "auth_permission" 
("name", "content_type_id", "codename") VALUES (E'Can add Clip', 64, E'add_clip')
2010-03-29 14:29:23 SAST ERROR:  duplicate key value violates unique 
constraint "auth_permission_pkey"
2010-03-29 14:29:23 SAST STATEMENT:  INSERT INTO "auth_permission" 
("name", "content_type_id", "codename") VALUES (E'Can add Clip', 64, E'add_clip')
2010-03-29 14:29:23 SAST LOG:  statement: ROLLBACK TO SAVEPOINT s1217227072_x1

Ok from that I could see that django was having trouble inserting entries into the auth_permissions table for my Clip model. The postgresql logging functions can prove indispensible when you just want to know what low level requests and responses are coming and going from your database.

  • jatorre

    Is there no way in django to enable debug mode to see what is really doing? In rails there is. Having to look at the db logs can be a bit of pain, specially if your db server is not close by.

  • Tim Sutton


    Yes the above is an edge case where the database has become inconsistent with the models. In general, django has very good debugging tools – if can debug out the individual sql statements that were called to render the page you are viewing. With django logging you can add your own comments etc into the log area. And you can granularly specify which hosts are allowed to see these debug messages which appear at the end of each rendered page.

    This is all done by changing a few flags in the file e.g.:

    108 # Added by Tim - tells django which clients may 
    109 # receive debug messages...
    INTERNAL_IPS = ('','')
    # Also added for logging disabled for prod machine
    DEBUG = False
    # Disabled for prod machine

    Just flipping those flags over to true will give nice debug info as described above.



  • Pingback: Adding files to the django auto refresh » /dev/stu