2016-11-04 49 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

相关问题