Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 6D7BE3405C8 for ; Wed, 22 Feb 2012 08:05:19 -0400 (AST) Received: from mail-we0-f174.google.com ([74.125.82.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S0AwQ-0005i3-6d for pgsql-sql@postgresql.org; Wed, 22 Feb 2012 12:05:18 +0000 Received: by werb13 with SMTP id b13so4444085wer.19 for ; Wed, 22 Feb 2012 04:04:48 -0800 (PST) Received-SPF: pass (google.com: domain of couling@gmail.com designates 10.180.81.66 as permitted sender) client-ip=10.180.81.66; Authentication-Results: mr.google.com; spf=pass (google.com: domain of couling@gmail.com designates 10.180.81.66 as permitted sender) smtp.mail=couling@gmail.com; dkim=pass header.i=couling@gmail.com Received: from mr.google.com ([10.180.81.66]) by 10.180.81.66 with SMTP id y2mr34409815wix.20.1329912288936 (num_hops = 1); Wed, 22 Feb 2012 04:04:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=UmuDkc1PzT2aNTeNmi3sLZioQLpN6PhduFziRdtETUM=; b=D55qQeUZavimNPrW7oGje8eA05JeFcx028rRbrf67zgeErdGoaWLFLAXuvcBY/clJW 6bF38WiZ5RB7U4NYqbKTWDwn6SmYDvdq8qwlz1SfN+cjGCb5eMQ8GXqTe5C0jgFoErOg HqMfBU8aogINgsaKhSode8KfNBG9g7jkOiIIc= Received: by 10.180.81.66 with SMTP id y2mr28480531wix.20.1329912288868; Wed, 22 Feb 2012 04:04:48 -0800 (PST) Received: from zbox.pedal.me.uk (cpc2-king5-0-0-cust1226.perr.cable.virginmedia.com. [82.36.220.203]) by mx.google.com with ESMTPS id fl2sm70302298wib.4.2012.02.22.04.04.47 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 22 Feb 2012 04:04:47 -0800 (PST) Received: from [192.168.42.192] (unknown [77.107.188.229]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by zbox.pedal.me.uk (Postfix) with ESMTPSA id 7CEC3198042A; Wed, 22 Feb 2012 12:04:49 +0000 (GMT) Message-ID: <4F44D9D9.3060201@pedal.me.uk> Date: Wed, 22 Feb 2012 12:04:41 +0000 From: Philip Couling User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 To: Gary Stainburn CC: pgsql-sql@postgresql.org Subject: Re: Another constant in foreign key problem. References: <201202221035.11963.gary.stainburn@ringways.co.uk> In-Reply-To: <201202221035.11963.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201202/70 X-Sequence-Number: 36363 Hi Gary In short you can't have a foreign key here because foreign keys are just that: a *key* from another table. fl_level is not a key, it is not unique and requires fl_f_id to be unique. If you want a foreign key between these two tables then you must add the facility id to the document library and use a composite key (fl_f_id , fl_level) as the foreign key. This may be advantageous as the same structure could be re-used across other facilities (even though it would not be applicable to all). There are a couple of alternatives to this. One is to add a separate column to facility_levels. The sole purpose of this column would be to proved a unique key on every row in the table. Foreign keys (such as on library_document_user_level) would reference this and not fl_level. There would be no constraint here to prevent a row in library_document_user_level from referencing the wrong facility. Another possible alternative which is very PostgreSQL specific is to use inheritance. Create a child table storing only facility_levels for fl_f_id 22. The foreign key would reference the child table and not facility_levels. Everything in the child table would also exist in facility_levels. As this is so specific to PostgreSQL and is not hugely common as a technique, read this as a *possible* solution not a recommended one. Hope this helps On 22/02/2012 10:35, Gary Stainburn wrote: > I have three tables, > > users - all users of my web site > facilities - facilities available on my web site > facility_levels - access levels per user/facility. > > One of my facilities is a document library (f_id = 22) > For this facility I have the levels > > select * from facility_levels where fl_f_id=22 order by fl_level; > fl_f_id | fl_level | fl_desc > ---------+----------+-------------- > 22 | 1 | Read Only > 22 | 2 | Add Versions > 22 | 3 | Amend > (3 rows) > > > This sets the global access level for the Document Library per user. > > I now want to add authentication control on a document or folder level. For > this I need to create a table library_document_user_level > > u_id - user id > ld_id - library document id > fl_level - level > > The foreign key constraint on fl_level needs to check facility_levels for > fl_f_id = 22 as well as fl_level existing. > > I've googled this but can't find a suitable solution. Can anyone help please.