Re: Table with Field Serial - Problem - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Table with Field Serial - Problem
Date
Msg-id 52726D58.9000908@gmail.com
Whole thread Raw
In response to Table with Field Serial - Problem  (Yostin Vargas <yostinv@gmail.com>)
Responses Re: Table with Field Serial - Problem
List 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.klaver@gmail.com


pgsql-general by date:

Previous
From: si24
Date:
Subject: Re: Explanantion on pgbouncer please
Next
From: si24
Date:
Subject: Re: Explanantion on pgbouncer please