Question

I have a full-text search query in PostgreSQL that looks like this:

to_tsvector('english', coalesce("products"."name"::text, '')) || to_tsvector('english', coalesce("products"."uid"::text, '')) || to_tsvector('english', coalesce("products"."serial"::text, ''));

But creating a migration like this does not work for some reason:

create_trigger(compatibility: 1).on(:products).before(:insert, :update) do
  "new.tsv_body := to_tsvector('english', coalesce("products"."name"::text, '')) || to_tsvector('english', coalesce("products"."uid"::text, '')) || to_tsvector('english', coalesce("products"."serial"::text, ''));"
end

Any advice?

Was it helpful?

Solution

The hint is in how Stack Overflow has syntax-highlighted your program text:

  "new.tsv_body := to_tsvector('english', coalesce("products"."name"::text, '')) .... )
                                                   ^^^^^^^^^^ ^^^^^^

You've included double-quotes around the table and column names. The whole string is in double quotes. So these inset double quotes end the string.

You'll need to escape them, or omit them. I don't do Ruby / Rails, but if it's anything like most languages, backslash escapes are appropriate:

"new.tsv_body := to_tsvector('english', coalesce(\"products\".\"name\"::text, '')) .... )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top