Skip to content

Instantly share code, notes, and snippets.

@ruckus
Created April 24, 2012 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruckus/2483910 to your computer and use it in GitHub Desktop.
Save ruckus/2483910 to your computer and use it in GitHub Desktop.
CREATE FUNCTION wine_books_search_content_trigger() RETURNS trigger AS $$
begin
new.search_content := (
-- MUST use COALLESCE here if any of these columns can be NULL
SELECT to_tsvector(COALESCE(wine_books.code, '') || ' ' || COALESCE(wine_books.name, '') || ' ' || COALESCE(producers.name, '') || ' ' || COALESCE(varietals.name, '') || COALESCE(wine_books.year::varchar, ''))
FROM wine_books
LEFT OUTER JOIN producers ON producers.id = wine_books.producer_id
LEFT OUTER JOIN varietals ON varietals.id = wine_books.varietal_id
WHERE wine_books.id = new.id);
return new;
end
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_search_content BEFORE INSERT OR UPDATE
ON wine_books FOR EACH ROW EXECUTE PROCEDURE wine_books_search_content_trigger();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment