Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 01:10 AM
Joolz
Guest
 
Posts: n/a
Default reference to a view

In my db I have a table type_of_action, fields code varchar, name
varchar, medical boolean. Two other tables refer to this table, one of
them to the medical rows, the other one to the none-medical rows. I
would like to make the reference with a view, like this:

create table type of action_type (
code varchar,
name varchar,
medical boolean
);

create view action_type_medical as select * from action_type
where medical=true;
create view action_type_non_medical as select * from action_type
where medical=false;

create table general_actions (
field1, field2, field_etc,
action_type varchar references action_type_non_medical(code)
);

create table medical_actions (
field1, field2, field_etc,
action_type varchar references action_type_medical(code)
);

But pg refuses this, can't create a reference to a view. I can now
create a trigger plus function that will do the check, but is there a
more elegant way? Thanks!

--
10:35-10:41
Fedora Core release 2 (Tettnang) Linux 2.6.6-1.435

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

  #2  
Old November 23rd, 2005, 01:11 AM
Stephan Szabo
Guest
 
Posts: n/a
Default Re: reference to a view

On Wed, 16 Jun 2004, Joolz wrote:
[color=blue]
> In my db I have a table type_of_action, fields code varchar, name
> varchar, medical boolean. Two other tables refer to this table, one of
> them to the medical rows, the other one to the none-medical rows. I
> would like to make the reference with a view, like this:
>
> create table type of action_type (
> code varchar,
> name varchar,
> medical boolean
> );
>
> create view action_type_medical as select * from action_type
> where medical=true;
> create view action_type_non_medical as select * from action_type
> where medical=false;
>
> create table general_actions (
> field1, field2, field_etc,
> action_type varchar references action_type_non_medical(code)
> );
>
> create table medical_actions (
> field1, field2, field_etc,
> action_type varchar references action_type_medical(code)
> );
>
> But pg refuses this, can't create a reference to a view. I can now
> create a trigger plus function that will do the check, but is there a
> more elegant way? Thanks![/color]

Not really right now. Note, that to do foreign keys properly you need
triggers on action_type as well and those triggers need to take into
account the view conditions to determine what checks to do.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

  #3  
Old November 23rd, 2005, 01:11 AM
Stephan Szabo
Guest
 
Posts: n/a
Default Re: reference to a view

On Wed, 16 Jun 2004, Joolz wrote:
[color=blue]
> In my db I have a table type_of_action, fields code varchar, name
> varchar, medical boolean. Two other tables refer to this table, one of
> them to the medical rows, the other one to the none-medical rows. I
> would like to make the reference with a view, like this:
>
> create table type of action_type (
> code varchar,
> name varchar,
> medical boolean
> );
>
> create view action_type_medical as select * from action_type
> where medical=true;
> create view action_type_non_medical as select * from action_type
> where medical=false;
>
> create table general_actions (
> field1, field2, field_etc,
> action_type varchar references action_type_non_medical(code)
> );
>
> create table medical_actions (
> field1, field2, field_etc,
> action_type varchar references action_type_medical(code)
> );
>
> But pg refuses this, can't create a reference to a view. I can now
> create a trigger plus function that will do the check, but is there a
> more elegant way? Thanks![/color]

Not really right now. Note, that to do foreign keys properly you need
triggers on action_type as well and those triggers need to take into
account the view conditions to determine what checks to do.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

  #4  
Old November 23rd, 2005, 01:11 AM
Joolz
Guest
 
Posts: n/a
Default Re: reference to a view

> [Stephan Szabo schreef op 16-06-2004 07:57 -0700][color=blue]
>
> On Wed, 16 Jun 2004, Joolz wrote:
>[color=green]
> > In my db I have a table type_of_action, fields code varchar, name
> > varchar, medical boolean. Two other tables refer to this table,
> > one of them to the medical rows, the other one to the none-medical
> > rows. I would like to make the reference with a view, like this:
> >
> > create table type of action_type (
> > code varchar,
> > name varchar,
> > medical boolean
> > );
> >
> > create view action_type_medical as select * from action_type
> > where medical=true;
> > create view action_type_non_medical as select * from action_type
> > where medical=false;
> >
> > create table general_actions (
> > field1, field2, field_etc,
> > action_type varchar references action_type_non_medical(code)
> > );
> >
> > create table medical_actions (
> > field1, field2, field_etc,
> > action_type varchar references action_type_medical(code)
> > );
> >
> > But pg refuses this, can't create a reference to a view. I can now
> > create a trigger plus function that will do the check, but is
> > there a more elegant way? Thanks![/color]
>
> Not really right now. Note, that to do foreign keys properly you
> need triggers on action_type as well and those triggers need to take
> into account the view conditions to determine what checks to do.[/color]

Thanks for pointing that out. Considering this, it may be a better
idea after all to make two (physically) seperate tables after all.
After all, these are "only" domain tables, and I don't want the
reverse-update trigger to change any of my "real" data.

--
22:40-22:43
Fedora Core release 2 (Tettnang) Linux 2.6.6-1.435

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

  #5  
Old November 23rd, 2005, 01:11 AM
Joolz
Guest
 
Posts: n/a
Default Re: reference to a view

> [Stephan Szabo schreef op 16-06-2004 07:57 -0700][color=blue]
>
> On Wed, 16 Jun 2004, Joolz wrote:
>[color=green]
> > In my db I have a table type_of_action, fields code varchar, name
> > varchar, medical boolean. Two other tables refer to this table,
> > one of them to the medical rows, the other one to the none-medical
> > rows. I would like to make the reference with a view, like this:
> >
> > create table type of action_type (
> > code varchar,
> > name varchar,
> > medical boolean
> > );
> >
> > create view action_type_medical as select * from action_type
> > where medical=true;
> > create view action_type_non_medical as select * from action_type
> > where medical=false;
> >
> > create table general_actions (
> > field1, field2, field_etc,
> > action_type varchar references action_type_non_medical(code)
> > );
> >
> > create table medical_actions (
> > field1, field2, field_etc,
> > action_type varchar references action_type_medical(code)
> > );
> >
> > But pg refuses this, can't create a reference to a view. I can now
> > create a trigger plus function that will do the check, but is
> > there a more elegant way? Thanks![/color]
>
> Not really right now. Note, that to do foreign keys properly you
> need triggers on action_type as well and those triggers need to take
> into account the view conditions to determine what checks to do.[/color]

Thanks for pointing that out. Considering this, it may be a better
idea after all to make two (physically) seperate tables after all.
After all, these are "only" domain tables, and I don't want the
reverse-update trigger to change any of my "real" data.

--
22:40-22:43
Fedora Core release 2 (Tettnang) Linux 2.6.6-1.435

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles