Postgres创建的模型实例中的主键默认值为nil

时间:2016-11-04 09:26:39

标签: ruby-on-rails postgresql ruby-on-rails-4 activerecord

我使用的是rails 4.2& postgresql和我正在使用现有的非rails DB。表模式如下:

# == 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

1 个答案:

答案 0 :(得分:0)

在你的模型中,你告诉Rails主键列是CustomerNo,而不是id?

    SELECT DISTINCT t."BP Name"
FROM (  SELECT
             "BP Name",
             "Disbursment Date"
    FROM  "SB_MD" 
    WHERE    year("Disbursment Date")  = (Year(getdate()) -1)
) t LEFT JOIN(  SELECT "BP Name"
    FROM  "SB_MD" 
    WHERE    year("Disbursment Date")  < (year(getdate()) -1)
) t1 ON t."BP Name"  = t1."BP Name"  
WHERE    t1."BP Name"  is null