我正在寻找一种在 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
任何人都可以帮助指导我解决这个问题并让我了解我做错了什么吗?我需要修复它,因为这个特定的迁移阻止执行 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)
谢谢! (如果我需要提供更多代码,请告诉我。)
最佳答案
您应该在创建新版本之前删除旧表:
def self.up
drop_table :examples
create_table :examples do |t|
#...
end
end
并且由于您无法真正反转那个 drop_table
,您可能希望在回滚中引发异常:
def self.down
raise ActiveRecord::IrreversibleMigration
end
或者您可能只想保留当前的 self.down
。
关于ruby-on-rails - 阻止 rake db :migrate 的 Drop_table 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811748/