今朝、db:seed:dumpを使用してデータベースを復元しました。単純なrakeファイルを実行しようとすると、次のエラーが発生します。
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "reports_pkey"
DETAIL: Key (id)=(1) already exists.
: INSERT INTO "reports" ("worker_id", "report_month", "status", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
/path/to/app/dir/lib/tasks/produce.rake:22:in `block (3 levels) in <top (required)>'
/path/to/app/dir/lib/tasks/produce.rake:17:in `each'
/path/to/app/dir/lib/tasks/produce.rake:17:in `block (2 levels) in <top (required)>'
Caused by:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "reports_pkey"
DETAIL: Key (id)=(1) already exists.
/path/to/app/dir/lib/tasks/produce.rake:22:in `block (3 levels) in <top (required)>'
/path/to/app/dir/lib/tasks/produce.rake:17:in `each'
/path/to/app/dir/lib/tasks/produce.rake:17:in `block (2 levels) in <top (required)>'
Tasks: TOP => produce:report_month
(See full trace by running task with --trace)
私はこのrakeタスクを何十回もエラーなしで実行しましたが、唯一の違いは最近バックアップを展開したことです。
エラーを読み取ると、Railsはすでに占有されているID間隔を1に戻そうとしているようです。
seed:dump gemは、次のような構造を持つseeds.rbファイルを作成します。
Note.create!([
{id: 1, notable_id: 51, notable_type: "Worker", body: "Some sort of text here", created_at: "2017-10-27 00:27:21", updated_at: "2017-10-27 00:27:21"},
{id: 2, notable_id: 68, notable_type: "Worker", body: "another note", created_at: "2017-11-11 18:43:13", updated_at: "2017-11-11 18:43:13"}
])
誰かがこれに遭遇したことがありますか?修正する方法はありますか?
回答:データベースのバックアップにdb:seed:dumpgemを使用しないでください。少なくとも私がそれを使っていた方法ではありません。
pg_dumpコマンドで作成した以前のバックアップから復元することになりました。
コメント投稿者の皆さん、ありがとうございました
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加