pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steve Crawford <scrawford@pinpointresearch.com>
To: Marc Fromm <Marc.Fromm@wwu.edu>
Cc: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: Re: primary key and insert
Date: Thu, 11 Dec 2008 16:39:43 -0800
Message-ID: <4941B2CF.1040906@pinpointresearch.com> (raw)
In-Reply-To: <B0D7C0A3F35FE144A70312D086CBCA9B0213C0B95D@ExchMailbox2.univ.dir.wwu.edu>
References: <B0D7C0A3F35FE144A70312D086CBCA9B0213C0B95D@ExchMailbox2.univ.dir.wwu.edu>

Marc Fromm wrote:
> I created this table:
> ...
>        id         | integer                       | not null
> Indexes:
>     "alert_list_pkey" PRIMARY KEY, btree (id)
> I get this error when I run the insert a listed below. The insert does 
> not have an entry for the primary key "id" since I thought it updates 
> automatically:
> *Warning*: pg_query() [function.pg-query 
> <http://finaid46.finaid.wwu.edu/lan/student_alerts/function.pg-query;]: 
> Query failed: ERROR: null value in column "id" violates not-null 
> constraint
> ...
> How do I do an insert on this table and have the primary key "id" 
> update with the record?
>  
I think you are confusing primary key and serial.

A column can be a primary key (not-null, unique row id) or a serial 
(actually automatically creates an integer type, a sequence, and a 
default for the column of nextval('sequence') to automatically generate 
a new value for each record).

A primary key could be a serial, but doesn't have to be.

A serial can be a primary key but doesn't have to be.

Note: due to things like transaction rollbacks, a serial column will 
have unique IDs generated but they are not guaranteed to be consecutive.

Cheers,
Steve




view thread (4+ messages)  latest in thread

Message-ID: <4941B2CF.1040906@pinpointresearch.com>
Permalink:  ../4941B2CF.1040906@pinpointresearch.com/
Also on:    postgresql.org/message-id/4941B2CF.1040906@pinpointresearch.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: scrawford@pinpointresearch.com, Marc.Fromm@wwu.edu
  Subject: Re: primary key and insert
  In-Reply-To: <4941B2CF.1040906@pinpointresearch.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox