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
I was having a similar problem and this was the only page I could find. So just in case it will be of help to anyone else...
I started suddenly getting missing primary key messages on a couple tables. I'm guessing, but not sure, that this started happening after pushing data (pg_dump local, heroku pg:restore
)
The primary keys in question were both on tables that had been renamed so that the pkey name did not match the table name--but on the other hand lots of other renamed tables were in the same boat and did not have problems.
Anyway, at one point in my flailing around I tried uploading another dump file and I noticed some complaints on the offending indices. First it would try to delete them and complain that it couldn't because they did not exist. Later it would try to create them and complain that it couldn't because they already existed.
Very annoying considering that pkey info doesn't show up in schema.rb
and is supposed to 'just work', right?
Anyway, what worked for me (and thus the reason I'm posting) is to do a heroku pg:reset
and then load the dump again. Side note, I got 'internal server error' the first two times I tried heroku pg:reset
. But later I tried again and it worked.