Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating two tables in a many-to-one relationship through a view in PostgreSQL
    primarykey
    data
    text
    <p>I have two tables: <code>foos</code> and <code>bars</code>, and there is a many-to-one relationship between them: each <code>foo</code> can have many <code>bars</code>. I also have a view <code>foobars</code>, which joins these two tables (its query is like <code>select foo.*, bar.id from foos, bars where bar.foo_id=foo.id</code>).</p> <p>EDIT: You would not be wrong if you said that there's a many-to-many relationship between <code>foo</code>s and <code>bar</code>s. A <code>bar</code>, however, is just a tag (in fact, it is a size), and consists just of its name. The table <code>bars</code> has the same role as a link table would have.</p> <p>I have a rule on inserting to <code>foobars</code> such that the “foo” part is inserted to <code>foos</code> as a new row, and “bar” part, which consists of a couple of bar-id's separated by commas is split, and for each such part a link between it and the appropriate <code>foo</code> is created (I use a procedure to do that).</p> <p>This works great for inserts. I have a problem, however, when it comes to updating the whole thing. The <code>foo</code> part of the rule is easy. However, I don't know how to deal with the multiple <code>bar</code>s part. When I try to do something like <code>DELETE FROM bars WHERE foo_id=new.foo_id</code> in the rule, I end deleting everything from the table <code>bars</code>.</p> <p>What am I doing wrong? Is there a way of achieving what I need? Finally, is my approach to the whole thing sensible?</p> <p>(I do this overcomplicated thing with the view because the data I get is in the form of “<code>foo</code> and all its <code>bar</code>s”, but the user must see just <code>foobars</code>.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload