获取PG :: UndefinedFunction:生产中的错误

时间:2015-04-07 10:37:42

标签: ruby-on-rails postgresql

我有一个客户类

class Customer < ActiveRecord::Base
  enum status: [:unconfirmed]

  default_scope { order updated_at: :desc }

  scope :unconfirmed, -> { where(status: 0) }
end

Schema中的Status字段定义为整数,默认值为0.

在开发中,SQLite一切正常,但在生产中,PostgresSQL,当我尝试运行 Customer.unconfirmed 时,我收到错误:

PG::UndefinedFunction: ERROR:  operator does not exist: boolean = integer
LINE 1: ...s".* FROM "customers"  WHERE "customers"."status" = 0  ORDER...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "customers".* FROM "customers"  WHERE "customers"."status" = 0  ORDER BY "customers"."updated_at" DESC
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: boolean = integer
LINE 1: ...s".* FROM "customers"  WHERE "customers"."status" = 0  ORDER...

有人可以帮我找到这里发生的事情吗?

1 个答案:

答案 0 :(得分:1)

要使用enum功能,必须需要制作enum field as integer。但是根据你得到的例外情况,很明显你将字段status设为布尔

您可以检查生产服务器中的列类型。以rails c production打开rails控制台。然后Customer.columns_hash['status'].type查看列的类型。

布尔添加到change the column type的新迁移到整数,然后应用迁移。 然后你可以这样做:

scope :unconfirmed, -> { where(status: :unconfirmed) }
#or
scope :unconfirmed, -> { where(status: 0) }