繁体   English   中英

Drop_table问题阻止rake db:migrate

[英]Drop_table problem preventing rake db:migrate

我一直在寻找一种在Rails中删除表并重新开始的方法,并遇到以下答案: Rails DB迁移-如何删除表?

但是,当我运行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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM