ruby-on-rails - 获取 PG::UndefinedFunction: ERROR in production

标签 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...

谁能帮我看看这是怎么回事?

最佳答案

要使用枚举功能,您必须需要制作enum field as integer .但是根据您得到的异常,很明显您将字段 status 设置为 boolean

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

将新迁移添加到 change the column typebooleaninteger,然后应用迁移。 然后你可以这样做:

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

关于ruby-on-rails - 获取 PG::UndefinedFunction: ERROR in production,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29489547/

相关文章:

不同主要版本之间的 PostgreSQL 复制

c++ - PostgreSQL 向 C++ 应用程序发出警报

sql - 我可以通过编写 SQL 而不是 ActiveRecord 来节省内存吗?

sql - 使用默认值重载 Postgres 函数 - 我的函数是不可调用的吗?

ruby-on-rails - 如果有的话,rspec 相对于 test::unit 的主要优势是什么?

ruby-on-rails - 在 rails 3.2 中使用 paypal 预先批准的付款

ruby-on-rails - 调试 redis、rails 和 sidekiq

ruby-on-rails - Heroku 上偶尔出现 Rack::Timeout::RequestTimeoutException

ruby-on-rails - 如何在Rails中计算 "days with records"?

sql - PostgreSQL - 合并两个表