Getting “Unknown primary key for table” while the ID is there

Seems primary key is missing for the table collections.

Prior to Rails 3.2, set the primary key in model like

class Collection < ActiveRecord::Base
  set_primary_key "my_existing_column"
end

In Rails 3.2+ and Rails 4, set the primary key in model like

class Collection < ActiveRecord::Base
  self.primary_key = "my_existing_column"
end

OR

We can alter the table and set the primary key for id like

Create a migration file to set the primary key

class AddPrimaryKeyToCollections < ActiveRecord::Migration
 def change
   execute "ALTER TABLE collections ADD PRIMARY KEY (id);"
 end
end

Leave a Comment