Getting “Unknown primary key for table” while the

2019-01-26 02:56发布

I've been debugging this strange problem of Rails giving me "Unknown primary key for table...", even when the table's ID is there.

I've copied the database from one heroku app to another, on the original databse there is no problem and the new one gives me a db error.

This is the error:

ProductsController# (ActionView::Template::Error) "Unknown primary key for table collections in model Collection."

/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:366:in `primary_key'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:480:in `association_primary_key'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:58:in `block in add_constraints'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `each_with_index'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `add_constraints'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:31:in `scope'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:98:in `association_scope'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:87:in `scoped'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:573:in `first_or_last'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:105:in `last'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:in `last'
/app/app/helpers/likes_helper.rb:62:in `significant_liker'

The line that causes it:

product.collections.last.try :user

and the table:

d8apjspa441pad=> \d collections
                                     Table "public.collections"
     Column     |          Type          |                        Modifiers                         
----------------+------------------------+----------------------------------------------------------
 id             | integer                | not null default nextval('collections_id_seq'::regclass)
 name           | character varying(255) | 
 user_id        | integer                | 
 permalink      | character varying(255) | 
 category_id    | integer                | 
 products_count | integer                | 
 is_featured    | boolean                | 
Indexes:
    "index_lists_on_user_id_and_permalink" UNIQUE, btree (user_id, permalink)

Any idea why this might happen?

Thanks!

9条回答
老娘就宠你
2楼-- · 2019-01-26 03:19

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

查看更多
爷、活的狠高调
3楼-- · 2019-01-26 03:21

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.

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

I tried a few tips 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 was simple: 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.

查看更多
疯言疯语
4楼-- · 2019-01-26 03:25

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)"
查看更多
不美不萌又怎样
5楼-- · 2019-01-26 03:33

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
查看更多
三岁会撩人
6楼-- · 2019-01-26 03:34

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

reindex index $primary_key_index_name

查看更多
混吃等死
7楼-- · 2019-01-26 03:34

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'...
查看更多
登录 后发表回答