2

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) 

ありがとう!(さらにコードを提供する必要がある場合は、お知らせください。)

4

1 に答える 1

7

新しいバージョンを作成する直前に、古いテーブルを削除する必要があります。

def self.up
    drop_table :examples
    create_table :examples do |t|
        #...
    end
end

そして、それを実際に元に戻すことはできないdrop_tableため、ロールバックで例外を発生させることができます。

def self.down
    raise ActiveRecord::IrreversibleMigration
end

または、現在の状態を維持したいだけかもしれませんself.down

于 2011-07-25T04:51:26.067 に答える