2

我正在寻找一种在 Rails 中删除表并重新开始的方法,并遇到了这个答案:Rails DB Migration - How To Drop a Table?

但是,当我运行时,出现drop_table :examples以下错误:

-bash: drop_table: command not found

这是我的create_examples迁移文件:

def self.down
  drop_table :examples
end

谁能帮助指导我解决这个问题并让我了解我做错了什么?我需要修复它,因为这个特定的迁移阻止执行 a rake db:migrate,产生以下错误:

==  CreateExamples: migrating ====================================================
-- create_table(:examples)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "examples" already exists: CREATE TABLE "examples" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime) 

谢谢!(如果我需要提供更多代码,请告诉我。)

4

1 回答 1

7

您应该在创建新版本之前删除旧表:

def self.up
    drop_table :examples
    create_table :examples do |t|
        #...
    end
end

而且由于您无法真正扭转它drop_table,因此您可能希望在回滚中引发异常:

def self.down
    raise ActiveRecord::IrreversibleMigration
end

或者,也许您只是想保留当前的self.down​​.

于 2011-07-25T04:51:26.067 回答