Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Sequence in PostgreSQL is not updated when doing manual insert [RSS Feed]

#1 Nov. 23, 2005 19:00:36

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Sequence in PostgreSQL is not updated when doing manual insert


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?

Offline

  • Root
  • » Django
  • » Sequence in PostgreSQL is not updated when doing manual insert [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of April
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support