This is a build on a question I asked before (without any luck).
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.
My questions are:
- Any idea what's going on here?
- Do you think it's right that I'm using employment_id as the primary key?
- Would help if I deleted the 'id' field?
- Is there any other best practice you would recommend?
Thanks for your help!
Derek.
Edit:
Adding migration file as requested:
class CreateEmployees < ActiveRecord::Migration
def change
create_table :employees do |t|
t.string :employment_id, :unique => true
etc...
end
end
end
EDIT
Very kind of nobody to point this out, but it's clear to me now that the real answer is "Just because you can, doesn't mean you should!"