Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UQeaf-0005QO-2o for pgsql-sql@arkaria.postgresql.org; Fri, 12 Apr 2013 14:04:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UQeae-00058p-Hg for pgsql-sql@arkaria.postgresql.org; Fri, 12 Apr 2013 14:04:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UQeXb-0004x5-J6 for pgsql-sql@postgresql.org; Fri, 12 Apr 2013 14:01:23 +0000 Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UQeXW-0000yg-7Q for pgsql-sql@postgresql.org; Fri, 12 Apr 2013 14:01:23 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UQeXT-0001G7-LX for pgsql-sql@postgresql.org; Fri, 12 Apr 2013 16:01:15 +0200 Received: from ip-118-90-25-130.xdsl.xnet.co.nz ([118.90.25.130]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 12 Apr 2013 16:01:15 +0200 Received: from jasen by ip-118-90-25-130.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 12 Apr 2013 16:01:15 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: Restrict FOREIGN KEY to a part of the referenced table Date: 12 Apr 2013 13:53:19 GMT Organization: Dis (not Dat) Organisation Lines: 32 Message-ID: References: <3358559.iSkkklqms2@hek506> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-25-130.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -4.3 (----) 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 On 2013-04-11, Matthias Nagel wrote: > Hello, > > is there any best practice method how to create a foreign key that only allows values from those rows in the referenced table that fulfill an additional condition? tes. make the key wide enough to capture this state. I dom't like it either. or partition the child table (which may not work for other constraints) > First I present two pseudo solutions to clarify what I would like to > do. They are no real solutions, because they are neither SQL standard > nor postgresql compliant. The third solution actually works, but I do > not like it for reason I will explain later: > FOREIGN KEY ( parent_id, 42 ) REFERENCES parent ( id, discriminator ) I have wanted this before too. > FOREIGN KEY ( parent_id ) REFERENCES ( SELECT * FROM parent WHERE discri I hadn't thought of expressing it like that. or similarly using a view instead of a select. but I think I have tried FOREIGN KEY ( parent_id ) REFERENCES parent ( id ) where discriminator = 42 and it didn't work. -- ⚂⚃ 100% natural -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql