pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Johnston <polobo@yahoo.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #9088: default are not working
Date: Tue, 4 Feb 2014 09:32:32 -0800 (PST)
Message-ID: <1391535152473-5790511.post@n5.nabble.com> (raw)
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>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Christian Kruse-4 wrote
> 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:

I'll admit I've occasionally wished for the ability for the system to
convert any supplied literal NULL into the default but that is not how it
works.  Specifying NULL explicitly means that is what you want and if the
column is constrained to be non-NULL the system will be unable to fulfill
your request and throw an error.

You may be able to use a BEFORE INSERT trigger to check for the presence of
NULL in the column in question and manually issue the "DEFAULT" expression
yourself (I do not believe the trigger can say "use the default" to set a
value) though I'm not sure on the specifics as to when the NULL check
occurs.

David J.





--
View this message in context: http://postgresql.1045698.n5.nabble.com/BUG-9088-default-are-not-working-tp5790410p5790511.html
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (6+ messages)

Message-ID: <1391535152473-5790511.post@n5.nabble.com>
Permalink:  ../1391535152473-5790511.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1391535152473-5790511.post@n5.nabble.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 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: polobo@yahoo.com
  Subject: Re: BUG #9088: default are not working
  In-Reply-To: <1391535152473-5790511.post@n5.nabble.com>
  References: <20140203222323.13575.44279@wrigleys.postgresql.org> <52F018B5.3070106@joh.to> <CADMm_6BOKJ=4vNCAu7KfMQ2potAFmxqsDpsGBWpm2p4RyGe=vA@mail.gmail.com> <20140204081310.GC10459@defunct.ch> <1391535152473-5790511.post@n5.nabble.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