ruby-on-rails - Rails 没有为新记录分配唯一 ID

标签 ruby-on-rails postgresql

今天早上我使用 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:dump gem 进行数据库备份。至少不是我使用它的方式。

我最终从通过 pg_dump 命令创建的早期备份中恢复。

感谢所有评论者

关于ruby-on-rails - Rails 没有为新记录分配唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48570046/

相关文章:

sql - 如何使用PostgreSQL显示数据库查询中的记录

ruby-on-rails - 如何使用 rails 创建指向 pdf 文件的链接?

MySQL hell ...无法连接到数据库/tmp/mysql.sock

sql - INNER JOIN on UPDATE with WHERE 更新 Postgres 中的每条记录

postgresql - 传递一个函数名作为参数

c# - SqlBuilder 返回错误结果

javascript - Rails - 如何在发出请求之前修改 http 参数?

ruby-on-rails - Rails 中 -%> 和 %> 的区别

mysql - 转换为 Float 返回 ActiveRecord::StatementInvalid: Mysql2::Error

sql - 如何规范化/更新 "order"列