pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christian Kruse <christian@2ndQuadrant.com>
To: Patrick Lademan <mjfrog14@gmail.com>
Cc: Marko Tiikkaja <marko@joh.to>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #9088: default are not working
Date: Tue, 4 Feb 2014 09:13:10 +0100
Message-ID: <20140204081310.GC10459@defunct.ch> (raw)
In-Reply-To: <CADMm_6BOKJ=4vNCAu7KfMQ2potAFmxqsDpsGBWpm2p4RyGe=vA@mail.gmail.com>
References: <20140203222323.13575.44279@wrigleys.postgresql.org>
	<52F018B5.3070106@joh.to>
	<CADMm_6BOKJ=4vNCAu7KfMQ2potAFmxqsDpsGBWpm2p4RyGe=vA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Hi,

On 03/02/14 18:38, Patrick Lademan wrote:
> -- Test Case
> drop table default_test;
> 
> 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
> ( null, null, null, null );
> 
> select * from default_test;
> 
> ERROR:  null value in column "userid" violates not-null constraint
> DETAIL:  Failing row contains (null, null, null, null).
> ********** Error **********
> 
> 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,

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

view thread (6+ messages)  latest in thread

Message-ID: <20140204081310.GC10459@defunct.ch>
Permalink:  ../20140204081310.GC10459@defunct.ch/
Also on:    postgresql.org/message-id/20140204081310.GC10459@defunct.ch

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: christian@2ndQuadrant.com, mjfrog14@gmail.com, marko@joh.to
  Subject: Re: BUG #9088: default are not working
  In-Reply-To: <20140204081310.GC10459@defunct.ch>
  References: <20140203222323.13575.44279@wrigleys.postgresql.org> <52F018B5.3070106@joh.to> <CADMm_6BOKJ=4vNCAu7KfMQ2potAFmxqsDpsGBWpm2p4RyGe=vA@mail.gmail.com> <20140204081310.GC10459@defunct.ch>

* 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