Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W5mjy-0001Bi-9W for pgsql-sql@arkaria.postgresql.org; Wed, 22 Jan 2014 01:36:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W5mjx-000189-OO for pgsql-sql@arkaria.postgresql.org; Wed, 22 Jan 2014 01:36:25 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W5mjw-000182-Jp for pgsql-sql@postgresql.org; Wed, 22 Jan 2014 01:36:24 +0000 Received: from prod2.officenet.no ([195.159.87.246]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W5mjs-0003ah-DG for pgsql-sql@postgresql.org; Wed, 22 Jan 2014 01:36:23 +0000 Received: from localhost ([127.0.0.1] helo=prod2) by prod2.officenet.no with esmtp (Exim 4.76) (envelope-from ) id 1W5mhm-0001fX-SW for pgsql-sql@postgresql.org; Wed, 22 Jan 2014 02:34:10 +0100 Date: Wed, 22 Jan 2014 02:34:09 +0100 (CET) From: Andreas Joseph Krogh To: "pgsql-sql@postgresql.org" Message-ID: Subject: Deferrable conditional unique constraints MIME-Version: 1.0 X-Mailer: OfficeNet Mail 1.9.0-SNAPSHOT X-Pg-Spam-Score: -2.5 (--) Content-Type: multipart/alternative; boundary="----=_Part_172_272926860.1390354449174" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org ------=_Part_170_958154330.1390354449160 Content-Type: multipart/related; boundary="----=_Part_171_1867439104.1390354449160" ------=_Part_171_1867439104.1390354449160 Content-Type: multipart/alternative; boundary="----=_Part_172_272926860.1390354449174" ------=_Part_172_272926860.1390354449174 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I know I can create a deferrable constraint trigger to accomplish this but = it=20 would be cool if one could do it with regular unique constraint. =C2=A0 I'm= trying=20 to enforce that a company can only have *one* "is_preferred" business-field= ,=20 where is_preferred is NOT NULL DEFAULT FALSE. =C2=A0 table company( id seri= al name=20 varchar ) =C2=A0 table businessfield_company( business_field_id FK business= field=20 company_id FK company ) =C2=A0 I can then add a conditional UNIQUE index to= enforce=20 that a company can only have *one* preferred=3Dtrue business-field: =C2=A0 = create=20 unique index company_bf_is_pref_idx on businessfield_company(business_field= _id,=20 company_id) where is_preferred; =C2=A0 However, I need it to be DEFERRABLE = INITIALLY=20 DEFERRED, which isn't possible (to my knowledge) with unique indexes. =C2= =A0 It is=20 possible to create deferrable unique constraints, but they cannot be=20 conditional: alter table businessfield_company add constraint=20 businessfield_company_is_pref_idx UNIQUE (company_id, is_preferred)where=20 is_preferred deferrable initially deferred; ERROR:=C2=A0 syntax error at or= near=20 "where" Is this possible using "standard syntax" or do I have to use constr= aint=20 triggers? =C2=A0 Thanks. =C2=A0 -- Andreas Joseph Krogh =C2=A0 =C2=A0 =C2=A0 mob: +47 9= 09 56 963 Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no Public key: http://home.officenet.no/~andreak/public_key.asc ------=_Part_172_272926860.1390354449174 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I know I can create a deferrable constraint trigger to accomplish this= but it would be cool if one could do it with regular unique constraint.
=C2=A0
I'm trying to enforce that a company can only have *one* "is_pref= erred" business-field, where is_preferred is NOT NULL DEFAULT FALSE.
=C2=A0
table company(
id serial
name varchar
)
=C2=A0
table businessfield_company(
business_field_id FK businessfield
company_id FK company
)
=C2=A0
I can then add a conditional UNIQUE index to enforce that a company ca= n only have *one* preferred=3Dtrue business-field:
=C2=A0
create unique index company_bf_is_pref_idx on businessfield_company(bu= siness_field_id, company_id) where is_preferred;
=C2=A0
However, I need it to be DEFERRABLE INITIALLY DEFERRED, which isn't po= ssible (to my knowledge) with unique indexes.
=C2=A0
It is possible to create deferrable unique constraints, but they canno= t be conditional:
alter table businessfield_company add constraint businessfield_company_is_p= ref_idx UNIQUE (company_id, is_preferred) where is_preferred deferra= ble initially deferred;
ERROR:=C2=A0 syntax error at or near "where"
Is this possible using "standard syntax" or do I have to use= constraint triggers?
=C2=A0
Thanks.
=C2=A0
--
Andreas Joseph Krogh <andreak@officenet.no>=C2=A0 =C2=A0 =C2=A0 mob: = +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no Public key: http://home.officenet.no/~andreak/public_key.asc
------=_Part_172_272926860.1390354449174-- ------=_Part_171_1867439104.1390354449160-- ------=_Part_170_958154330.1390354449160--