ruby-on-rails - 阻止 rake db :migrate 的 Drop_table 问题

标签 ruby-on-rails ruby-on-rails-3 rake drop-table

我正在寻找一种在 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/

相关文章:

objective-c - iOS 应用程序和持久登录(显然是特定于手机和特定于应用程序的问题)

ruby-on-rails - ruby rails : How can I add a css file with rails project?

gem - rails s:在任何来源中都找不到rake-10.4.2(Bundler::GemNotFound)

ruby-on-rails - Rails/Nginx 将 http 重定向到 https

ruby-on-rails - 在 Heroku 上一起使用 Resque、Puma 和 Scheduler

database - 与另一个模型或父模型的多态关联

ruby-on-rails - rake db :migrate produces "rake aborted! could not find table" error

ruby-on-rails - Rails + Rake : How to simulate running of rake task to see if it works?

Java网站到Rails,复制密码加密

ruby-on-rails - 如何在注册和身份验证失败时重定向 Devise?