Drop_tableの問題Rake DBを防ぐ:移動します
-
25-10-2019 - |
質問
私はレールにテーブルを落とし、新鮮なものを始めて、この答えに出くわす方法を探していました: 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
.
所属していません StackOverflow