[SQL] foreign key to multiple tables depending on another column's value

2013-05-29 Thread Rodrigo Rosenfeld Rosas
Sorry, I wasn't sure what list I should be sending this question to... I have a multi-tenant-like application. We have a fields tree that we call a template, with something like this: fields(id, parent_id, name) And several other related tables. Since we started to support multiple

Re: [SQL] reduce many loosely related rows down to one

2013-05-29 Thread Marc Mamin
SELECT id, (array_agg(rspid))[1] AS rspid,-- (1) for such cases, I have created an new aggregate function: SELECT firstnotnull(rspid) AS rspid, this avoid to collect first all rspid values to then keep only the first one... CREATE OR REPLACE FUNCTION public.first_agg_nn (

[SQL] Re: [GENERAL] foreign key to multiple tables depending on another column's value

2013-05-29 Thread Vick Khera
On Wed, May 29, 2013 at 9:58 AM, Rodrigo Rosenfeld Rosas rr.ro...@gmail.com wrote: I know I could use a trigger, or some check constraint maybe, to ensure the field exists upon insert (or update), but I can't ensure the database will become inconsistent in case I remove a mapped field from

[SQL] Re: [GENERAL] foreign key to multiple tables depending on another column's value

2013-05-29 Thread Rodrigo Rosenfeld Rosas
Em 29-05-2013 12:51, Vick Khera escreveu: On Wed, May 29, 2013 at 9:58 AM, Rodrigo Rosenfeld Rosas rr.ro...@gmail.com mailto:rr.ro...@gmail.com wrote: I know I could use a trigger, or some check constraint maybe, to ensure the field exists upon insert (or update), but I can't