postgresql trigger (strategy) to update table based on entries of another table -


i new postgresql (spoilt django orm!), , create trigger updates table based on entries of table.

so, have following table on schema:

collection_myblogs(id, col1,col2,title,col4,col5) 

..where field id autogenerated. now, have new table created so:

create table fulltext(id serial not null, content text not null); alter table fulltext add constraint fulltext_pkey primary key (id); 

and insert values collection_myblogs so:

insert fulltext(content) select title collection_myblogs; 

all fine far...i trigger on fulltext such fulltext updates new entries everytime collection_myblogs has new entry. so, attempted creating trigger following:

create trigger collection_ft_update before insert or update on collection_myblogs each row execute procedure ft_update(); 

now, not entirely sure should go on ft_update() function, , @ moment, have:

create function ft_update() returns trigger ' begin insert fulltext(content) select new.title; return new; end ' language plpgsql; 

..which works fine inserts not updates. i.e if update title of orginal column collections_myblog(title) appears new entry on fulltext unsure how deal ids here.

i ids i.e primary keys same on each table. so, idea me have fulltext(id, content) == collection_myblogs(id, title) - if makes sense. so, id , content should replicated collection_myblogs table. how 1 go achieving this?

my understanding can use trigger before insert or update on collection_myblogs , somehow maintain fulltext(id, content) == collection_myblogs(id, title)

i appreciate guidance on this.

there large number of ways handle problem. examples:

  1. use table inheritance create "interface" data (no trigger needed, abstract table ends functioning view). complicated territory though.

  2. use trigger approach , handle update , delete separately. big issue here if have 2 areas of text identical, update trigger needs able separate them.

there many others should started.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -