1

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?

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Derek Hill
  • 5,965
  • 5
  • 55
  • 74
  • This was a case of [second time lucky](http://stackoverflow.com/questions/11580613/should-i-delete-the-default-id-field-when-using-a-string-primary-key-in-rails). – Derek Hill Jul 26 '12 at 19:28

0 Answers0