2015-04-07 69 views
1

獲得PG :: UndefinedFunction:ERROR生產

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

    default_scope { order updated_at: :desc } 

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

在架構狀態字段定義爲整數,默認爲0

在發展中,SQLite的一類客戶,一切工作正常但在生產,PostgreSQL的,當我嘗試運行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... 

誰能幫我找出這裏發生了什麼?

+0

只是'狀態:0 :: Boolean'取代'狀態:0' –

回答

1

要使用enum功能,您需要必須使enum field as integer。但根據您得到的例外情況,很明顯,您已將字段status設置爲布爾值

您可以檢查生產服務器中的列類型。以rails c production打開您的導軌控制檯。然後Customer.columns_hash['status'].type查看列的類型。

布爾整數添加一個新的遷移change the column type,然後應用遷移。 那麼你可以做:

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

但狀態被設置爲整數,我已經add_column:客戶:狀態:整數,默認:0在我的遷移文件中 – Kocur4d

+0

@ Kocur4d查看* Production *環境中是否存在待定遷移。 –

+0

@ Kocur4d打開控制檯後,在你的作品中做rails'production' - >'Customer.columns_hash ['status'] .type'來檢查它的數據類型。 –