我一直在寻找一种在轨道上放桌子并开始新鲜的方法,并遇到了这个答案: 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归因
不隶属于 StackOverflow
scroll top