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