我正在尝试在 Rails 教程第 3 版中构建示例应用程序。在第11章中,它首先为微帖子添加了一个新的模型。但是,当运行迁移时,它会抛出以下错误
== 20141212145132 创建条目:迁移 ===========================================-- create_table(:微帖子(-- add_index(:microposts, [:user_id, :created_at](耙子中止了!标准错误:发生错误,此迁移和所有后续迁移已取消:
SQLite3::SQLException: 没有这样的表: main.microposts: CREATE INDEX "index_entries_on_user_id_and_created_at" on "microposts" ("user_id", "created_at"(/
尝试更改这些行
class CreateMicroposts < ActiveRecord::Migration # <==== convention
def change
create_table :microposts do |t|
t.text :content
t.references :user, index: true
t.timestamps
t.index [:user_id, :created_at] # <===
end
end
end
好的,我现在明白我做错了什么。
我在"def change"的末尾添加了"添加索引",而不是在它之后。这是现在正在工作的代码...
class CreateEntries < ActiveRecord::Migration
def change
create_table :entries do |t|
t.text :content
t.references :user, index: true
t.timestamps
end
add_index :entries, [:user_id, :created_at]
end
end