【问题标题】:Postgres primary key default value nil in created model instance创建模型实例中的 Postgres 主键默认值 nil
【发布时间】:2016-11-04 09:26:38
【问题描述】:

我正在使用 rails 4.2 和 postgresql,并且正在处理现有的非 rails 数据库。表模式如下:

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

知道为什么我会得到这个吗?

谢谢 尼利什

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 是的,我做了 self.primary_key = 'CustomerNo'
猜你喜欢
  • 2015-05-14
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多