【问题标题】:Non-integer primary key Rails indexing非整数主键 Rails 索引
【发布时间】:2014-09-02 04:33:02
【问题描述】:

有一个带有表 Content::Translation 的旧数据库,其中包含 textresource_idayah_key

resource_id = 整数, ayah_key = 字符串

我的模特:

class Content::Translation < ActiveRecord::Base
    extend Content

    self.table_name = 'translation'
    self.primary_keys = :ayah_key, :resource_id
end

当使用 Elasticsearch 建立索引时,它会创建一个 1000 的批处理查询,这会产生 PSQL 错误:

SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "content"."translation"  WHERE ("content"."translation"."ayah_key" >= 0) AND ("content"."translation"."resource_id" >= 0) LIMIT 1000) subquery_for_count
PG::UndefinedFunction: ERROR:  operator does not exist: text >= integer
LINE 1: ...ation"  WHERE ("content"."translation"."ayah_key" >= 0) AND ...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "content"."translation"  WHERE ("content"."translation"."ayah_key" >= 0) AND ("content"."translation"."resource_id" >= 0) LIMIT 1000) subquery_for_count
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: text >= integer
LINE 1: ...ation"  WHERE ("content"."translation"."ayah_key" >= 0) AND ...

这是因为我的 ayah_key 列是一个字符串,我确实需要该列是一个字符串。有没有一种方法可以让我编写一个范围或做一些事情,以便与&gt;= '0' 进行比较?

【问题讨论】:

    标签: ruby-on-rails postgresql activerecord


    【解决方案1】:

    如何为它提供一个方法,然后将其用作属性?

    def ayah_key_id
      ayah_key.to_i
    end
    

    【讨论】:

    • 我该怎么做?此外,字符串的结构是这样的:number:number 例如:1:12
    • 你的意思是你想要def ayah_composite ayak_key_id.to_s + resource_id.to_s end 什么的。不清楚你想要什么。
    • 发表了我的答案!感谢您的帮助
    【解决方案2】:

    这个 gem 为我解决了它,它允许非整数 find_eachfind_in_batches

    https://github.com/mojotech/batchelor

    【讨论】:

      猜你喜欢
      • 2014-07-13
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      相关资源
      最近更新 更多