Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the default primary key column in ruby on rails 4.0.+?

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).

like image 386
software knife Avatar asked Nov 23 '25 21:11

software knife


2 Answers

You can override the primary key like this

class Book < ActiveRecord::Base
  self.primary_key = 'author'
end
like image 120
MilesStanfield Avatar answered Nov 26 '25 10:11

MilesStanfield


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
like image 24
aldrien.h Avatar answered Nov 26 '25 09:11

aldrien.h