title: tsvector_update_trigger can't find tsvector column
body: <p>I'm working on a pretty simple blog in Ruby on Rails and is currently implementing the search functionality using pg_search for PostgreSQL full text search. Unfortunately, I'm having a problem with (I believe) my trigger for updating the tsvector column in the posts table. After several hours of searching, I've not been able to resolve the issue myself - even though I suspect it's easy if you've done similar things before.</p>
  
  <p>Anyway, the error reads as follows:</p>
  
  
  
  <pre class="lang-rb prettyprint-override"><code>irb(main):001:0&gt; post = FactoryGirl.build :post
  =&gt; #&lt;Post id: nil, title: "Optimized uniform infrastructure", body: "&lt;p&gt;Inventore consectetur culpa nulla eius voluptati...", published: true, created_at: "2012-09-09  04:08:51", updated_at: nil, tsv: nil&gt;
  irb(main):002:0&gt; post.save
  ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column "tsv" does not exist
      LINE 1: SELECT tsvector_update_trigger(tsv, 'pg_catalog.english', ti...
      QUERY:  SELECT tsvector_update_trigger(tsv, 'pg_catalog.english', title, body)
      CONTEXT:  PL/pgSQL function "posts_before_insert_update_row_tr" line 3 at assignment
      : INSERT INTO "posts" ("body", "created_at", "published", "title", "updated_at") VALUES
      ($1, $2, $3, $4, $5) RETURNING "id"
  </code></pre>
  
  <p>The tsv column is made in a migration like so:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class AddTsvToPosts &lt; ActiveRecord::Migration
      def change
          add_column :posts, :tsv, :tsvector
          add_index(:posts, :tsv, using: 'gin')
      end
  end
  </code></pre>
  
  <p>The trigger is defined using <a href="https://github.com/jenseng/hair_trigger" rel="nofollow">hair_trigger</a> in this way:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class Post &lt; ActiveRecord::Base
    include PgSearch
  
    pg_search_scope :search, against: [:title, :body],
      using: {
        tsearch: {
          dictionary: 'english',
          prefix: true,
          tsvector_column: 'tsv'
        }
      }
  
    trigger.before(:insert, :update) do
      "new.tsv := tsvector_update_trigger(tsv, 'pg_catalog.english', title, body);"
    end
  end
  </code></pre>
  
  <p>The whole thing produces the following sql schema (with <code>config.active_record.schema_format = :sql</code>)
  --
      -- PostgreSQL database dump
      --</p>
  
  <pre class="lang-rb prettyprint-override"><code>SET statement_timeout = 0;
  SET client_encoding = 'UTF8';
  SET standard_conforming_strings = on;
  SET check_function_bodies = false;
  SET client_min_messages = warning;
  
  --
  -- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
  --
  
  CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
  
  
  --
  -- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
  --
  
  COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
  
  
  SET search_path = public, pg_catalog;
  
  --
  -- Name: posts_before_insert_update_row_tr(); Type: FUNCTION; Schema: public; Owner: -
  --
  
  CREATE FUNCTION posts_before_insert_update_row_tr() RETURNS trigger
      LANGUAGE plpgsql
      AS $$
  BEGIN
      new.tsv := tsvector_update_trigger(tsv, 'pg_catalog.english', title, body);
      RETURN NEW;
  END;
  $$;
  
  
  SET default_tablespace = '';
  
  SET default_with_oids = false;
  
  --
  -- Name: posts; Type: TABLE; Schema: public; Owner: -; Tablespace: 
  --
  
  CREATE TABLE posts (
      id integer NOT NULL,
      title character varying(255),
      body text,
      published boolean,
      created_at timestamp without time zone,
      updated_at timestamp without time zone,
      tsv tsvector
  );
  
  
  --
  -- Name: posts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
  --
  
  CREATE SEQUENCE posts_id_seq
      START WITH 1
      INCREMENT BY 1
      NO MINVALUE
      NO MAXVALUE
      CACHE 1;
  
  
  --
  -- Name: posts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
  --
  
  ALTER SEQUENCE posts_id_seq OWNED BY posts.id;
  
  
  --
  -- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -; Tablespace: 
  --
  
  CREATE TABLE schema_migrations (
      version character varying(255) NOT NULL
  );
  
  
  --
  -- Name: id; Type: DEFAULT; Schema: public; Owner: -
  --
  
  ALTER TABLE ONLY posts ALTER COLUMN id SET DEFAULT nextval('posts_id_seq'::regclass);
  
  
  --
  -- Name: posts_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
  --
  
  ALTER TABLE ONLY posts
      ADD CONSTRAINT posts_pkey PRIMARY KEY (id);
  
  
  --
  -- Name: index_posts_on_tsv; Type: INDEX; Schema: public; Owner: -; Tablespace: 
  --
  
  CREATE INDEX index_posts_on_tsv ON posts USING gin (tsv);
  
  
  --
  -- Name: unique_schema_migrations; Type: INDEX; Schema: public; Owner: -; Tablespace: 
  --
  
  CREATE UNIQUE INDEX unique_schema_migrations ON schema_migrations USING btree (version);
  
  
  --
  -- Name: posts_before_insert_update_row_tr; Type: TRIGGER; Schema: public; Owner: -
  --
  
  CREATE TRIGGER posts_before_insert_update_row_tr BEFORE INSERT OR UPDATE ON posts FOR EACH ROW EXECUTE PROCEDURE posts_before_insert_update_row_tr();
  
  
  --
  -- PostgreSQL database dump complete
  --
  
  SET search_path TO "$user",public;
  
  INSERT INTO schema_migrations (version) VALUES ('20131230213035');
  
  INSERT INTO schema_migrations (version) VALUES ('20140115101632');
  
  INSERT INTO schema_migrations (version) VALUES ('20140115183846');
  </code></pre>
  
  <p>If someone could help me to figure out on why rails doesn't acknowledges the existance of the tsv column I would be <strong>soooooo</strong> happy! :)</p>