Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 2C5161337E8B for ; Wed, 5 Jan 2011 16:24:10 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 10885-02-4 for ; Wed, 5 Jan 2011 20:24:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtpcorp.com (smtpcorp.com [174.143.60.32]) by mail.postgresql.org (Postfix) with ESMTP id 062451338894 for ; Wed, 5 Jan 2011 15:55:03 -0400 (AST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=main; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:Message-ID:Date:Subject:From:To; bh=9MMAhFmKV3NxqLUsDF9v8Zn/COWpe8fa1DWWorZOqP4=; b=IIZswX2bTvQbczyoHVlikT1S7KOxrt/Va0xw5M+zx9ObepN8T6Cdrd6XTtCYg3iEaEza+jqaX0QMjnmloc8oRdPakoZrF12v6rbqScw2MLGNvj1VmaHo0FsXGdKjcx8g; Received: from ip-118-90-142-165.xdsl.xnet.co.nz ([118.90.142.165] helo=gonzo) by smtpcorp.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.70) id 1PaZRl-00053S-97; mid 144 for pgsql-sql@postgresql.org; Wed, 05 Jan 2011 19:55:02 +0000 Received: from news by gonzo with local (Exim 4.72) (envelope-from ) id 1PaZRX-0007b8-T8 for pgsql-sql@postgresql.org; Thu, 06 Jan 2011 08:54:47 +1300 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: foreign key question Date: 5 Jan 2011 19:54:47 GMT Organization: Dis (not Dat) Organisation Lines: 32 Message-ID: References: <201101050953.43830.gary.stainburn@ringways.co.uk> <201101051040.08575.gary.stainburn@ringways.co.uk> NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1294257287 27489 127.0.0.1 (5 Jan 2011 19:54:47 GMT) 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-Smtpcorp-Track: 19908019.1.31043320 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201101/7 X-Sequence-Number: 34954 On 2011-01-05, Gary Stainburn wrote: > On Wednesday 05 January 2011 09:53:43 Gary Stainburn wrote: >> Now I want to set up a new access level table specific to the itinerary, >> along the lines of >> >> u_id int4 not null references users(u_id) >> fl_level int4 not null references facility_levels(16, fl_level) >> >> Firstly, is this possible, and secondly how would I do it? > > I've managed a work-around by creating a column that defaults to 16 and then > used ALTER TABLE ADD CONSTRAINT to add a foreign key with two pairs of keys. > > This feels wrong though as my table now has a column that is ultimately > redundant, and worse can be changed to a wrong value. > > Ok, I've sorted the last bit by adding a check constraint to make sure it > always contains 16, but it still feels wrong. it feels wrong that's because it's not normalised, the column with the 16's probably should not be there. or possibly it should have rows with other values too. look at how this table is useful and look for a more general way to do it. -- ⚂⚃ 100% natural