Re: Table with Field Serial - Problem

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Yostin Vargas <yostinv(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Table with Field Serial - Problem
Date: 2013-10-31 14:46:48
Message-ID: 52726D58.9000908@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/31/2013 07:31 AM, Yostin Vargas wrote:
>
> I have a table with only one Field ID type Serial Autonumeric and is a
> PK, i want insert a new record but it show me error Not null violation,
> but if i put a value the first INSERT work correctly but the next
> Insert it Show me error Unique violation,
>
> So i try adding a new field in this table and put a value null to this
> field, and the ID Autonumeric work .
>
> Exist a way to do it only with a field, i'm working with PHP???

Some actual examples form you end would help:)

My guess is you are trying to insert a NULL value into the PK field
instead of just not inserting anything at all. An alternative is to use
the DEFAULT keyword. See below for example.

create table test_table(id_fld serial primary key, char_fld varchar);

test=> \d test_table
Table "public.test_table"
Column | Type | Modifiers

----------+-------------------+-------------------------------------------------------------
id_fld | integer | not null default
nextval('test_table_id_fld_seq'::regclass)
char_fld | character varying |
Indexes:
"test_table_pkey" PRIMARY KEY, btree (id_fld)

test=> INSERT INTO test_table (id_fld , char_fld) VALUES (NULL, 't');
ERROR: null value in column "id_fld" violates not-null constraint

test=> INSERT INTO test_table (char_fld) VALUES ('t');
INSERT 0 1

test=> INSERT INTO test_table (id_fld , char_fld) VALUES (DEFAULT, 't');
INSERT 0 1

>
>
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message si24 2013-10-31 14:49:58 Re: Explanantion on pgbouncer please
Previous Message si24 2013-10-31 14:46:44 Re: Explanantion on pgbouncer please