0

これが私のスキーマです

          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()

トリガー関数は次のとおりです。

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;
$$;

ただし、次のようにレコードを更新すると:

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

私は得る

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)

列名が正しいと確信しています。重要かどうかはわかりませんが、tsvector 列を追加した後、このように行を連結します (Rails 移行を使用しています)。

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

何かを見逃していましたか、それとも各列に独自の tsvector 列 (1 つに連結されていない) が必要ですか?

4

1 に答える 1