加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ActiveRecord find_each和Postgres

发布时间:2020-12-17 02:36:14 所属栏目:百科 来源:网络整理
导读:我收到以下错误: PGError: ERROR: operator does not exist: character varying = integerLINE 1: ...CT "games".* FROM "games" WHERE ("games"."uuid" = 0) ORDE... ^HINT: No operator matches the given name and argument type(s). You might need to
我收到以下错误:

PGError: ERROR:  operator does not exist: character varying >= integer
LINE 1: ...CT  "games".* FROM "games"  WHERE ("games"."uuid" >= 0) ORDE...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT  "games".* FROM "games"  WHERE ("games"."uuid" >= 0) ORDER BY "games"."uuid" ASC LIMIT 1000

当我尝试这样做时:

Game.find_each do |game|
  # ...
end

我的模型有一个字符串(UUID)主键:

class Game < ActiveRecord::Base
  self.primary_key = 'uuid'

  before_create do |game|
    game.uuid = UUIDTools::UUID.timestamp_create().to_s if game.uuid.blank?
  end
end

我不知道为什么ActiveRecord放入WHERE子句,但它完全没有必要和类型错误的原因(因为它是一个字符串列,而不是一个整数).

那么,我该如何避免这种情况呢?我应该在模型定义中添加一些内容吗?或者我应该避免使用find_each并使用其他方法吗?这是一个rake任务,只需查看所有条目并查找一些其他信息……

解决方法

似乎find_each有一个非数字主键的错误:

https://groups.google.com/group/compositekeys/browse_frm/month/2011-06

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读