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

佐手、 提交于 2019-11-29 09:04:01
Debadatt

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
elc

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.

I was recently encountered this error: "Unknown primary key for table", and like the question asker, it appeared after copying a database to a Heroku app. TL;DR: try restarting the app.

In my case, the source database had no error, so I was confident the table and primary key were fine.

I tried a few suggestions on this page, including starting from scratch with a heroku pg:reset, new pg_dump of the old database, and pgbackups:restore into the new database, then running migrations and seeding... nothing worked.

What finally solved my problem: restarting the app. The new app had many database migrations, and running heroku restart reloaded the schema and picked up the schema changes. This page from Heroku's documentation explains:

Running Rake Commands

After running a migration you’ll want to restart your app with heroku restart to reload the schema and pickup any schema changes.

What helped for me (happened on heroku after a db restore) is reindexing the primary key index:

reindex index $primary_key_index_name

I was restoring database dump from heroku to my local system and was getting this error..

ActiveRecord::UnknownPrimaryKey: ActiveRecord::UnknownPrimaryKey

I was restoring on existing database, so I dropped the database, created new database and then restore the dump and it worked for me

restarting the heroku server worked for me. Maybe it was the spring-preloader which was recognizing the empty db-schema, during the db-restorement

If you're trying to troubleshoot this issue make sure you check your logs carefully. I noticed an earlier error related to a js asset that wasn't being precompiled. This got lost in the stack of rendering messages.

Once I fixed the asset precompilation issue, the 'Unknown primary key for table' error was no longer thrown.

This was 100% definitely the only thing I changed.

I was having this problem and the issue turned out to be that my table somehow actually didn't have a primary key index. The solution was to create a migration that added a primary key:

execute "ALTER TABLE appointment_reminder_text ADD PRIMARY KEY (id)"

Thanks changing the index above worked for me. Just another quick note about how this error will manifest itself if there's a more complex relation involved:

ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR:  zero-length delimited identifier at or near """"
LINE 1: ...CT "users".* FROM "users"  WHERE "benefits"."" IN ('1'...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!