Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZX5fF-0003vM-4U for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Sep 2015 10:53:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZX5fE-0005Ah-N1 for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Sep 2015 10:53:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZX5fE-0005AI-0u for pgsql-bugs@postgresql.org; Wed, 02 Sep 2015 10:53:12 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZX5f6-0004XU-Ot for pgsql-bugs@postgresql.org; Wed, 02 Sep 2015 10:53:11 +0000 MIME-Version: 1.0 Subject: BUG #13606: CHECK clause in CREATE TABLE command To: pgsql-bugs@postgresql.org From: p.buongiovanni@net-international.com Date: Wed, 02 Sep 2015 10:52:44 +0000 Message-ID: <20150902105244.1378.92867@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 13606 Logged by: Piergiorgio Email address: p.buongiovanni@net-international.com PostgreSQL version: 9.2.4 Operating system: CENTOS 6.2 Description: As the CHECK clause specifies an expression producing a Boolean result, the _Dummy1_CK2 constraint is valid when I write this CREATE TABLE command: CREATE TABLE draft.Dummy1 ( cFlag char (1) NOT NULL DEFAULT 'N', cField varchar DEFAULT NULL, CONSTRAINT _Dummy1_CK1 CHECK (cFlag IN ('Y','N')), CONSTRAINT _Dummy1_CK2 CHECK ( cFlag = 'N' OR ( cFlag = 'Y' AND cField IS NOT NULL ) ) ); If now you inspect the SQL code of the table Dummy1 with pgAdmin, you see that the above SQL code has been changed in the following one: CREATE TABLE draft.dummy1 ( cflag character(1) NOT NULL DEFAULT 'N'::bpchar, cfield character varying, CONSTRAINT _dummy1_ck1 CHECK (cflag = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])), CONSTRAINT _dummy1_ck2 CHECK (cflag = 'N'::bpchar OR cflag = 'Y'::bpchar AND cfield IS NOT NULL) ) WITH ( OIDS=FALSE ); ALTER TABLE draft.dummy1 OWNER TO netdw_owner; As you can see brackets has been removed from the definition of the constraint _Dummy1_CK2. Clearly this doesn't work as I want. If I want to solve the problem I must write the CREATE TABLE command as below: CREATE TABLE draft.Dummy2 ( cFlag char (1) NOT NULL DEFAULT 'N', cField varchar DEFAULT NULL, CONSTRAINT _Dummy2_CK1 CHECK (cFlag IN ('Y','N')), CONSTRAINT _Dummy2_CK2 CHECK ( CASE WHEN cFlag = 'N' THEN TRUE ELSE cField IS NOT NULL END ) ); -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs