Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WAjrU-0002qI-Cn for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Feb 2014 17:32:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WAjrS-0007aj-UC for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Feb 2014 17:32:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WAjrR-0007ac-Vz for pgsql-bugs@postgresql.org; Tue, 04 Feb 2014 17:32:38 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WAjrO-0003xY-VA for pgsql-bugs@postgresql.org; Tue, 04 Feb 2014 17:32:37 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WAjrM-0006oz-GX for pgsql-bugs@postgresql.org; Tue, 04 Feb 2014 09:32:32 -0800 Date: Tue, 4 Feb 2014 09:32:32 -0800 (PST) From: David Johnston To: pgsql-bugs@postgresql.org Message-ID: <1391535152473-5790511.post@n5.nabble.com> In-Reply-To: <20140204081310.GC10459@defunct.ch> References: <20140203222323.13575.44279@wrigleys.postgresql.org> <52F018B5.3070106@joh.to> <20140204081310.GC10459@defunct.ch> Subject: Re: BUG #9088: default are not working MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.8 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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