1

We have some imported data from a 3rd party which provides non-integer unique ids.

In Rails 2.2.2 we were able to use :foreign_key on our has_many relationships with a non-integer column and it worked.

But we are now upgrading to Rails 2.3.8 and it seems to force the foreign_key to an integer. Has anyone found a way to get this working?

Brian Armstrong
  • 19,707
  • 17
  • 115
  • 144

1 Answers1

1

According to this answer, the procedure has to be completed using execute:

create_table :employees, {:id => false} do |t|
  t.string :emp_id
  t.string :first_name
  t.string :last_name
end
execute "ALTER TABLE employees ADD PRIMARY KEY (emp_id);"

And as Sean McCleary mentioned, your ActiveRecord model should set the primary key using set_primary_key:

class Employee < ActiveRecord::Base
  set_primary_key :emp_id
  ...
end
Community
  • 1
  • 1
bbonamin
  • 30,042
  • 7
  • 40
  • 49
  • This answer talks about having a non-integer primary_key, what about foreign key? What about using t.references and Rails creating a another_table_id as integer? I ended up using t.string another_table_id instead of using t.references. – Eduardo Dec 18 '12 at 19:20