I have an already existing database schema with tables that have a string column as primary key and also some tables with more than one columns as key. I would like to map this schema in rails, but I dont know how to override the default primary key (a column id created by the rails framework).
Asked
Active
Viewed 2,619 times
2 Answers
3
You can override the primary key like this
class Book < ActiveRecord::Base
self.primary_key = 'author'
end

MilesStanfield
- 4,571
- 1
- 21
- 32
-
I read about this approach in my book and it says thats override only the column name, for instance the default column name for the priamry key in rails is id and now its author, but the datatype is still integer. I have a string as priamry key in my schema, therefore this solves not my problem . – software knife Mar 01 '16 at 19:25
-
[Altering the primary key in Rails to be a string](http://stackoverflow.com/questions/750413/altering-the-primary-key-in-rails-to-be-a-string#answer-6714889) and why you should try and steer yourself away from doing it – MilesStanfield Mar 01 '16 at 19:52
1
I don't know what you're trying to do. It's a mistake altering primary key in Rails.
But for that matter try to do it in your migration.
class Foos < ActiveRecord::Migration
def self.up
create_table :foos, :id => false do |t|
t.string :my_id
t.timestamps
end
end
end

aldrien.h
- 3,437
- 2
- 30
- 52