In order to load some test data into the database, I'm using the
following code:

    from django.core.db import db
    cursor = db.cursor()

    cursor.execute("insert into cities (id, name) values (1,
'Chicago');")
    cursor.execute("insert into cities (id, name) values (2,
'Miami');")
     ...

This works fine, but then when I try to use django later to add to this
table, I get an error:

    cities.City(name='Madison').save()

    ERROR: duplicate key violates unique constraint "cities_pkey"

I've tracked down the problem, and it seems that when I do my manual
inserts, the sequence (cities_id_seq) that PostgreSQL uses for primary
keys isn't updated.  Therefore, when I try to save my object with
django, the primary key collides with an already existing key.

I've implemented a simple fix by manually settings the sequence value
after I do my inserts, but I'm wondering why django/postgresql doesn't
do the right thing.  Has anyone seen this before?

Reply via email to