Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 495DB632DFD for ; Mon, 11 Jan 2010 15:50:08 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 86292-03 for ; Mon, 11 Jan 2010 19:49:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-gw30.mailanyone.net (smtp-gw30.mailanyone.net [208.70.128.56]) by mail.postgresql.org (Postfix) with ESMTP id 6BEAA632483 for ; Mon, 11 Jan 2010 15:49:57 -0400 (AST) Received: from mailanyone.net by smtp-gw30.mailanyone.net with esmtpsa (TLSv1:AES128-SHA:128) (MailAnyone extSMTP andy-lists@networkmail.eu) id 1NUQGu-0006it-G0 for pgsql-sql@postgresql.org; Mon, 11 Jan 2010 13:49:56 -0600 From: Andy Shellam Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: CHECK constraint removing brackets Date: Mon, 11 Jan 2010 19:49:50 +0000 Message-Id: To: pgsql-sql@postgresql.org Mime-Version: 1.0 (Apple Message framework v1077) X-Mailer: Apple Mail (2.1077) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.589 tagged_above=-10 required=5 tests=AWL=0.010, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/18 X-Sequence-Number: 33554 Hi, I notice this had been raised as a bug (and subsequently over-ruled) so = I'm asking how I can achieve the following business rule. I have an order table which has an invoice_id column that links to an = invoice table (an order can only have 1 invoice, but one invoice can = have multiple orders.) An order can have either an unconfirmed state, or any other state after = it's been confirmed. If an order has the state unconfirmed, the = invoice_id column must be null, as an invoice won't have been created = yet. If an order has any other state except unconfirmed, the invoice_id = must not be null. With the above in mind, I decided on the following check to enforce = this: (state =3D 'Unconfirmed'::client.order_state AND invoice_id =3D NULL) OR = (state !=3D 'Unconfirmed'::client.order_state AND invoice_id !=3D NULL) However PostgreSQL (8.4.2) converts this to the following: state =3D 'Unconfirmed'::client.order_state AND invoice_id =3D = NULL::integer OR state <> 'Unconfirmed'::client.order_state AND = invoice_id <> NULL::integer This allows both an order state of "unconfirmed" and a non-null = invoice_id, and an order state of "confirmed" and a NULL invoice_id. How can I achieve the above? Thanks, Andy=