postgresql trigger on tsvector column get ERROR: column “does not exist”

早过忘川 提交于 2019-12-13 04:48:12

问题


Here's my schema

          Column          |            Type            
--------------------------+----------------------------
 id                       | integer                    
 title                    | character varying(255)     
 summary                  | character varying(255)     
 readable_content         | text                       
 created_at               | timestamp without time zone
 updated_at               | timestamp without time zone
 textsearchable_index_col | tsvector                   

Indexes:
    "site_articles_pkey" PRIMARY KEY, btree (id)
    "index_site_articles_on_textsearchable_index_col" gin (textsearchable_index_col)
Triggers:
    site_articles_before_insert_update_row_tr BEFORE INSERT OR UPDATE ON site_articles FOR EACH ROW EXECUTE PROCEDURE site_articles_before_insert_update_row_tr()

and here's the trigger function:

CREATE FUNCTION site_articles_before_insert_update_row_tr() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
    new.tsv := tsvector_update_trigger(textsearchable_index_col, 'pg_catalog.simple', title, summary, readable_content);
    new.tsv := setweight(to_tsvector('pg_catalog.simple', coalesce(new.title,'')), 'A') ||
                setweight(to_tsvector('pg_catalog.simple', coalesce(new.summary,'')), 'B') ||
                setweight(to_tsvector('pg_catalog.simple', coalesce(new.readable_content,'')), 'C');
    RETURN NEW;
END;
$$;

However, when I update a record like this:

UPDATE "site_articles" SET "updated_at" = '2013-12-13 05:43:59.802580' WHERE "site_articles"."id" = 1

I get

ERROR:  column "textsearchable_index_col" does not exist
LINE 1: SELECT tsvector_update_trigger(textsearchable_index_col, 'pg...
                                       ^
QUERY:  SELECT tsvector_update_trigger(textsearchable_index_col, 'pg_catalog.simple', title, summary, readable_content)

I'm pretty sure the column name is correct. Not sure if it matters, I concatenate the rows like this after I added the tsvector column (I'm using Rails migration)

def up
  add_column :site_articles, :textsearchable_index_col, :tsvector

  sql = <<-SQL
    UPDATE site_articles SET textsearchable_index_col =
                   to_tsvector('simple', coalesce("site_articles"."title"::text,'')
                               || ' ' || coalesce("site_articles"."summary"::text, '')
                               || ' ' || coalesce("site_articles"."readable_content"::text, '')
                              );
  SQL
  execute sql

  add_index :site_articles, :textsearchable_index_col, using: 'gin'
end

Did I miss something or each column should have its own tsvector column (not concatenated in one)?


回答1:


Indeed. It should be new.textsearchable_index_col. The same for subsequent fields.

(Btw, there's little point in assigning tsv twice. Either use your first (corrected) statement, or your second. But not both, since the second overrides the first, and a tsv is expensive to compute.)



来源:https://stackoverflow.com/questions/20556255/postgresql-trigger-on-tsvector-column-get-error-column-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!