我得到以下错误:
2.4.1 :001 > Blogger.last
ActiveRecord::IrreversibleOrderError: Relation has no current order and table has no primary key to be used as default order from (irb):1
问题出在哪里?我该如何解决?
进一步资料:
Blogger.First似乎奏效了:
Blogger.first
Blogger Load (3.6ms) SELECT "bloggers".* FROM "bloggers" LIMIT $1 [["LIMIT", 1]]
# <Blogger id: 1, blogger_id: 502, deadline: nil, reminded: nil, needsreminder: nil, created_at: "2018-03-13 12:10:09", updated_at: "2018-03-13 12:10:09", interests: "Ethik, Gesellschaft, Kultur, Logik, Wissen, Wissen...">
博客是用一颗种子创造出来的:
Blogger.create!([
{id: 1, blogger_id: 502, deadline: nil, reminded: nil, needsreminder: nil, interests: "Ethik, Gesellschaft, Kultur, Logik, Wissen, Wissenschaft"},
{id: 2, blogger_id: 453, deadline: nil, reminded: nil, needsreminder: nil, interests: "Ethik, Gesellschaft, Kultur"},
{id: 3, blogger_id: 434, deadline: nil, reminded: nil, needsreminder: nil, interests: "Menschsein und Sprache"},
{id: 4, blogger_id: 370, deadline: nil, reminded: nil, needsreminder: nil, interests: "Ethik, Gesellschaft, Kultur"},
{id: 5, blogger_id: 485, deadline: nil, reminded: nil, needsreminder: nil, interests: "Unbekannt"}
])
以下是Bloger的迁移:
class CreateBloggers < ActiveRecord::Migration[5.1]
def change
create_table :bloggers do |t|
t.integer :blogger_id
t.date :deadline
t.date :reminded
t.boolean :needsreminder
t.timestamps
end
end
end
class AddInterestsToBloggers < ActiveRecord::Migration[5.1]
def change
add_column :bloggers, :interests, :string
end
end
Blogger.rb
class Blogger < ApplicationRecord
end
轨道:5.1.4
Ruby:2.4.1
PostgreSQL:x86_64-PC-LINUX-GNU上的PostgreSQL 9.5.12,GCC(Ubuntu 5.4.0-6ubuntu1~16.04.9)5.4.0 20160609,64位
3条答案
按热度按时间agxfikkp1#
我回滚我的迁移,然后再次迁移它们。
之后,错误不会再次出现:
然而,如果有人能解释一下如果发生这种错误该怎么办,我还是会很高兴的。
vlju58qv2#
我只是重新指定了主键,而没有提到模型文件中的任何内容。
注意:我实际上使用一个现有的表创建了一个新表,并出现了这个错误。
在创建表之后,我在迁移文件中指定了以下内容,
execute "ALTER TABLE items ADD PRIMARY KEY (id);"
bxfogqkk3#
我刚刚添加了
primary_key
,它起作用了: