How to isolate id from string primary key in Heroku Postgres

对着背影说爱祢 提交于 2020-01-15 10:44:08

问题


I have a model where I'm using a string as a primary key:

class Employee < ActiveRecord::Base
   self.primary_key = "employment_id"
end 

This table also contains the Rails default 'id' field, with a uniqueness constraint.

When I add a new employee locally it all works fine, with Rails automatically generating a new unique id.

However when I run this on Heroku Postgres it appears to treat 'id' and 'employment_id' as the same field. I tried to get around this by manually setting a unique id, but still get this behaviour:

Employee.new do |s|

  max_id = Employee.maximum(:id)  
  puts max_id.to_s             # => 1803 

  s.employment_id = "fred_01"      
  s.id = max_id + 1 

  puts employment_id.to_s      # => 1804

end

I'm running postgres 9.1.3 locally (and Heroku is on 9.1.4). I'm on Rails 3.2.3.

Any idea what might be going on? One idea I had was to get rid of the 'id' column, which is now redundant, but I'm worried about more pain down the road. What would you advise?

来源:https://stackoverflow.com/questions/11494417/how-to-isolate-id-from-string-primary-key-in-heroku-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!