0

我在 PostgreSQL 中有一个全文搜索查询,如下所示:

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

但是由于某种原因,创建这样的迁移不起作用:

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

有什么建议吗?

4

1 回答 1

1

提示在于 Stack Overflow 如何对您的程序文本进行语法高亮显示:

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

您已经在表名和列名周围添加了双引号。整个字符串用双引号括起来。所以这些插入的双引号结束了字符串

你需要逃避它们,或者忽略它们。我不做 Ruby / Rails,但如果它像大多数语言一样,反斜杠转义是合适的:

"new.tsv_body := to_tsvector('english', coalesce(\"products\".\"name\"::text, '')) .... )
于 2014-01-21T02:26:55.850 回答