Created
April 24, 2012 21:20
-
-
Save ruckus/2483910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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