Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 90336632429 for ; Tue, 9 Feb 2010 08:01:24 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 50659-05 for ; Tue, 9 Feb 2010 12:01:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.pcl-ipout02.plus.net (relay.pcl-ipout02.plus.net [212.159.7.100]) by mail.postgresql.org (Postfix) with ESMTP id B62226326B9 for ; Tue, 9 Feb 2010 07:59:17 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAKLccEtUXebi/2dsb2JhbADZI4RUBA Received: from relay03.plus.net ([84.93.230.226]) by relay.pcl-ipout02.plus.net with ESMTP; 09 Feb 2010 11:59:15 +0000 Received: from [84.51.143.99] (helo=server3.office.archonet.com) by relay03.plus.net with esmtp (Exim) id 1NeokN-0000OU-66 for pgsql-sql@postgresql.org; Tue, 09 Feb 2010 11:59:15 +0000 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTPA id E5549274075 for ; Tue, 9 Feb 2010 11:59:14 +0000 (GMT) Message-ID: <4B714E12.1090207@archonet.com> Date: Tue, 09 Feb 2010 11:59:14 +0000 From: Richard Huxton User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8pre) Gecko/20100129 Lightning/1.0b1 Shredder/3.0.2pre MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: 'image' table with relationships to different objects References: <20100209074909.GA11838@apartia.fr> In-Reply-To: <20100209074909.GA11838@apartia.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Plusnet-Relay: c4cec2bd9e0a56e85ad5f8b395fde2bf X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.034 tagged_above=-10 required=5 tests=AWL=-1.035, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201002/34 X-Sequence-Number: 33633 On 09/02/10 07:49, Louis-David Mitterrand wrote: > Hello, > > In my database I have different object types (person, location, event, > etc.) all of which can have several images attached. > > What is the best way to manage a single 'image' table with relationships > to (potentially) many different object types while keeping referrential > integrity (foreign keys)? The "clean" way to do this would be with a number of joining tables: images (img_id, file_name, title ...) persons (psn_id, first_name, last_name, ...) locations (loc_id, loc_name, lat, lon, ...) events (evt_id, evt_name, starts_on, ends_on, ...) person_images (psn_id, img_id) location_images (loc_id, img_id) event_images (evt_id, img_id) You might then want a view over these joining tables to see what images go where... CREATE VIEW all_images AS SELECT i1.img_id, i1.file_name, 'PERSON'::text AS link_type, p.first_name || ' ' || p.last_name AS linked_name FROM images i1 JOIN person_images pi ON i1.img_id = pi.img_id JOIN persons p ON pi.psn_id = p.psn_id UNION ALL SELECT i2.img_id, i2.file_name, 'LOCATION'::text AS link_type, l.loc_name AS linked_name FROM images i2 JOIN location_images li ON i2.img_id = li.img_id JOIN locations l ON li.loc_id = l.loc_id ... You could do something clever with inheritance on the joining tables, but it's better to keep things simple imho. -- Richard Huxton Archonet Ltd