Skip site navigation (1) Skip section navigation (2)

Re: Noob question: how to auto-increment index field on INSERT?

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Noob question: how to auto-increment index field on INSERT?
Date: 2009-11-19 17:06:50
Message-ID: he3tva$68n$1@ger.gmane.org (view raw or flat)
Thread:
Lists: pgsql-novice
Ken MacDonald wrote on 19.11.2009 17:55:
> Hi,
> I have a PostgreSQL DB created by a Django model, with a field 'id' that 
> is automatically created by Django as a primary key, type integer.
> 
> I would like to create a new row by doing something like....
> 
> INSERT INTO table (a, b, c, d) VALUES ('aa', 'bb', 'cc', 'dd')
> 
> where I've been hoping that 'id' would get the next value of id 
> available. Unfortunately, instead I get a 'duplicate primary key' error 
> saying that 'id' is a duplicate, even though I'm not specifying it 
> explicitly in the INSERT. What is the proper way to auto-increment a 
> primary key?

You should create the column with the datatype serial 

Thomas


In response to

pgsql-novice by date

Next:From: APseudoUtopiaDate: 2009-11-19 17:19:23
Subject: Re: Noob question: how to auto-increment index field on INSERT?
Previous:From: Ken MacDonaldDate: 2009-11-19 16:55:49
Subject: Noob question: how to auto-increment index field on INSERT?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group