Re: BUG #9088: default are not working - Mailing list pgsql-bugs

From Christian Kruse
Subject Re: BUG #9088: default are not working
Date
Msg-id 20140204081310.GC10459@defunct.ch
Whole thread Raw
In response to Re: BUG #9088: default are not working  (Patrick Lademan <mjfrog14@gmail.com>)
Responses Re: BUG #9088: default are not working
List pgsql-bugs
Hi,

On 03/02/14 18:38, Patrick Lademan wrote:
> -- Test Case
> drop table default_test;
>=20
> create table default_test
> (
>   userId   varchar(20)   default 'test' not null,
>   date1    timestamp     default now() not null,
>   date2    timestamp     default current_timestamp not null,
>   date3    timestamp     default localtimestamp not null
> );
>=20
> insert into default_test
> ( userId, date1, date2, date3 )
> values
> ( null, null, null, null );
>=20
> select * from default_test;
>=20
> ERROR:  null value in column "userid" violates not-null constraint
> DETAIL:  Failing row contains (null, null, null, null).
> ********** Error **********
>=20
> ERROR: null value in column "userid" violates not-null constraint
> SQL state: 23502
> Detail: Failing row contains (null, null, null, null).

This won't work and didn't work in earlier versions. When you
explicitly set the columns to NULL the default values don't apply. You
have to leave them out or to explicitly request the default values:

create table default_test
(
  userId   varchar(20)   default 'test' not null,
  date1    timestamp     default now() not null,
  date2    timestamp     default current_timestamp not null,
  date3    timestamp     default localtimestamp not null
);

insert into default_test (userId, date1, date2, date3) values
  (DEFAULT, DEFAULT, DEFAULT, DEFAULT);
insert into default_test (userId) values ('x');
select * from default_test;
 userid |           date1            |           date2            |        =
   date3
--------+----------------------------+----------------------------+--------=
--------------------
 test   | 2014-02-04 09:10:28.587693 | 2014-02-04 09:10:28.587693 | 2014-02=
-04 09:10:28.587693
 x      | 2014-02-04 09:11:05.15543  | 2014-02-04 09:11:05.15543  | 2014-02=
-04 09:11:05.15543
(2 rows)

Maybe you are mixing things up with MySQL, where NULL values trigger
default values.

Best regards,

--=20
 Christian Kruse               http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #9087: Foreign data wrapper connection management issues
Next
From: Greg Stark
Date:
Subject: Re: WTF