当我尝试运行rake db:migrate时,为什么一直出现此错误?

时间:2012-05-08 05:30:43

标签: ruby-on-rails migration dbmigrate ancestry

==  AddAncestryToMessages: migrating ==========================================
-- add_column(:messages, :ancestry, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: no such table: messages: ALTER TABLE "messages" ADD "ancestry" varchar(255)

所以我的应用程序有消息,你可以发布(有点像推特)和我添加回复,我正在使用祖先宝石这样做。

我的schema.rb文件中的代码(我认为这是每次运行rake db:migrate时用来创建表的文件。但我可能是错的(这可能是问题!)

  create_table "messages", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "ancestry"
  end

  add_index "messages", ["user_id", "created_at", "ancestry"], :name =>   "index_messages_on_user_id_and_created_at_and_ancestry"

1 个答案:

答案 0 :(得分:0)

哦,现在我明白了:您已将代码添加到schema.rb,但您必须使用迁移。从schema.rb删除手动添加的代码并运行:

rails g migration CreateMessages

您将获得文件db / migrate / [timestamp] _create_messages.rb。使用您的代码填充其change方法然后运行(应该为您创建,但是为空。对于旧版本,它名为up):

rake db:migrate

此命令将自行更改schema.rb不要手动更改它!(至少要成为成熟的Rails程序员)。