2016-11-04 59 views
0

我正在使用rails 4.2 & postgresql和我正在使用現有的非rails數據庫。表架構如下:Postgres主鍵默認值在創建的模型實例中爲零

# == Schema Information 
# 
# Table name: customer 
# 
# CustomerNo   :text    default("P'::text || nextval('sqcustomer_no"), not null, primary key 
# CustomerOperatorId :string(8) 
# CustomerType   :integer 
# BrokerId    :string(250) 
# Address    :text 
# SalutationId   :integer 

的問題是:

customer = Customer.create! #successfully created 
customer.id #nil value 
customer.CustomerNo #nil value 

我使用的是2個擴展postgis, plpgsqlCoutomer.count增加正常。我試過customer.reload它給我錯誤。

customer實例變量

#<Customer CustomerNo: "P'::text || nextval('sqcustomer_no", CustomerOperatorId: nil, CustomerType: nil, BrokerId: nil, Address: nil, SalutationId: nil> 

爲什麼我得到這個任何想法的輸出?

感謝 Neelesh

+0

這裏有什麼用? http://stackoverflow.com/questions/5975081/camelcase-instead-of-snake-case-in-rails-db –

+0

@DavidAldridge我做了,但沒有工作。 – Neelesh

回答

0

在你的模型,你告訴Rails的主鍵列CustomerNo,而不是ID?

class Customer < ActiveRecord::Base 
    set_primary_key :customer_no 
    ... 
end 
+0

是的,我做了self.primary_key ='CustomerNo' – Neelesh

相關問題