Re: many-to-many relationship - Mailing list pgsql-sql

From Steve Midgley
Subject Re: many-to-many relationship
Date
Msg-id 20081009191653.99AF137BD49@postgresql.org
Whole thread Raw
In response to many-to-many relationship  (Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>)
List pgsql-sql
At 05:20 PM 10/8/2008, pgsql-sql-owner@postgresql.org wrote:
>Date: Wed, 8 Oct 2008 11:25:10 +0200
>From: Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
>To: pgsql-sql@postgresql.org
>Subject: Re: many-to-many relationship
>Message-ID: <20081008092510.GA23361@apartia.fr>
>Mail-Followup-To: pgsql-sql@postgresql.org
>References: <20081007132118.C80641E81548@mx2.hub.org> 
><20081008001705.88A7B5B93F8F9@zenon.apartia.fr>
>In-Reply-To: <20081008001705.88A7B5B93F8F9@zenon.apartia.fr>
>X-Archive-Number: 200810/23
>X-Sequence-Number: 31665
>
> >
> > |id|image_url|f_table|f_key
> > |1 |url......|person |1234
> > |2 |url2.....|event  |5678
> >
> > I think this is called a "polymorphic join" but I could be wrong 
> about
> > that. I'd guess you could construct a rule or trigger to validate 
> the
> > foreign key data on insert/update but that's out of my skill area.
>
>Hi Steve,
>
>So in your solution the f_table column is just text which needs to be
>validated by a custom trigger?

Hi,

Yup - that's exactly what I'm suggesting. Storing the text value of the 
related tables right in the table in question. It might seem insane, 
but in my experience it works out reasonably well. Ruby on Rails has 
popularized the approach, using it both in the data backend, as well as 
in the OO frontend (so Rugy object class to be instantiated is chosen 
by the text value of "f_table" for a given row - hence the 
polymorphism).

http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations

There are some situations where this approach could create problems but 
if in general all you're doing is select statements along these lines:

select * from images where
f_table = 'person' and f_id = '1234'

There's not much to go wrong. (Famous last words).

And regarding the custom validation by trigger, I'd think that would 
work just fine. I'm not an expert on triggers, rules and constraints in 
Pg though. (I do all my validation in the middleware, which might give 
some people here high blood pressure). :)

Keep us posted on which solution you choose and how it works out for 
you!

Steve 



pgsql-sql by date:

Previous
From: "Relyea, Mike"
Date:
Subject: Re: sequence number in a result
Next
From: "Bart Degryse"
Date:
Subject: Re: trigger parameters, what am I doing wrong ??