| From: | Enrico Weigelt <weigelt(at)metux(dot)de> | 
|---|---|
| To: | pgsql-general(at)postgresql(dot)org | 
| Subject: | Re: Foreign key to a view (UNION of two or more tables), | 
| Date: | 2005-06-29 11:36:29 | 
| Message-ID: | 20050629113629.GB15893@nibiru.borg.metux.de | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
* Karl O. Pinc <kop(at)meme(dot)com> wrote:
<snip>
> So the problem then is that there are codes (e.g. cities) that are
> used by multiple questions, sometimes optional or N/A is allowed
> and sometimes not.
For such cases you could introduce another layer, like a datatype.
Each question can be answered with some datatype, and may optionally
be empty - just like rows in a database :). 
CREATE TABLE q_types 
(
    id		oid	default nextval('q_type_id'),
    description	text,
    ..
);
CREATE TABLE q_type_values
(
    type_id	oid,
    answer_id	oid,
    title	text,
    ...,
    PRIMARY KEY(type_id, answer_id)
);
CREATE TABLE q_question
(
    id		oid	default nextval,
    qtype	oid	references q_types (id),
    question	text
);
CREATE TABLE q_answer
(
    user_id	oid	references q_user(id),
    question_id oid	references q_question(id),
    value       oid
);
...
Maintaining the integrity of q_answer.value is a little bit more 
complicated. I don't know if its possible with an foreign key, 
since it spans over multiple tables ( question_id->qtype + value )
You probably need an hand-written trigger.
Unanswered questions (or selected n/a) could be marked by simply
setting value to NULL. You also could introduce a separate flag
in q_answer for that.
BTW: its probably not such a bad idea to present the whole 
stuff as one writable view to the frontend and let the database
do the logic of mapping answer texts <-> ids.
cu
-- 
---------------------------------------------------------------------
 Enrico Weigelt    ==   metux IT service
  phone:     +49 36207 519931         www:       http://www.metux.de/
  fax:       +49 36207 519932         email:     contact(at)metux(dot)de
---------------------------------------------------------------------
  Realtime Forex/Stock Exchange trading powered by postgresSQL :))
                                            http://www.fxignal.net/
---------------------------------------------------------------------
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Enrico Weigelt | 2005-06-29 11:56:04 | faking writable views as tables | 
| Previous Message | Enrico Weigelt | 2005-06-29 11:09:12 | Re: rule as on insert to view with multiple fk referencing the same table |