迁移后Rails Postgres错误:tsvector列不存在

时间:2017-12-17 21:23:42

标签: ruby-on-rails postgresql full-text-search tsvector

我正在使用Rails运行Postgres版本9.3.4.2。我迁移了数据库并从模型tsvector中删除了Post列(并将其添加到Message)。现在当我点击Post控制器的create操作并尝试INSERT INTO "posts"时,我收到错误"tsvector column 'tsv_body' does not exist"。架构显示tsvector没有posts列。我重新启动了服务器和Postgres。发生了什么事?我认为从技术上讲,错误信息是正确的 - tsvectortsv_body不存在!这并不意味着。但是,为什么它首先在tsvector模型中寻找Post列?

修改

好的,我在Railscasts ep结束时发现了这个。我认为问题是由这段代码引起的:

class AddTsvectorSearchToPosts < ActiveRecord::Migration[5.1]
  def change
    # Adds a tsvector column for the body
    add_column :posts, :tsv_body, :tsvector

    # Adds an index for this new column
    execute <<-SQL
      CREATE INDEX index_posts_tsv_body ON posts USING gin(tsv_body);
    SQL

    # Updates existing rows so this new column gets calculated
    execute <<-SQL
      UPDATE posts SET tsv_body = (to_tsvector('english', coalesce(content, '')));
    SQL

    # Sets up a trigger to update this new column on inserts and updates
    execute <<-SQL
      CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
      ON posts FOR EACH ROW EXECUTE PROCEDURE
      tsvector_update_trigger(tsv_body, 'pg_catalog.english', content);
    SQL
  end
end

当有sql迁移时,显然schema.rb文件不完整。所以问题现在变成:如何编写迁移以撤消此代码?

2 个答案:

答案 0 :(得分:0)

看起来tsvector是postgres中的一种数据,即列类型而不是列名,或者也与索引有关。如果您可以发布您的代码,那么人们可能会提供帮助。否则,this可能会给你更多线索

答案 1 :(得分:0)

是的,架构文件不完整。原来在structure.sql中显示正确的数据库结构。确实在那里,CREATE TRIGGER表上有Post个代码。此迁移修复了它:

def change
  execute <<-SQL
    DROP INDEX IF EXISTS index_posts_tsv_body;
    DROP TRIGGER IF EXISTS tsvectorpostupdate ON posts;
  SQL
end
相关问题