在 postgres 数据库中创建新记录时出现问题。使用 Heroku Cedar 堆栈和 Rails 3.2。
调用创建结果
ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "answer_selections_pkey"
这是我的问题表架构(它应该使用隐式自动增量 id 列):
create_table "answer_selections", :force => true do |t|
t.integer "individual_id"
t.integer "answer_choice_id"
t.boolean "still_true", :default => false
end
这是调用创建时的错误。
2012-09-07T14:46:44+00:00 app[web.1]: ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "answer_selections_pkey"
2012-09-07T14:46:44+00:00 app[web.1]: : INSERT INTO "answer_selections" ("answer_choice_id", "individual_id", "still_true") VALUES ($1, $2, $3) RETURNING "id"):
2012-09-07T14:46:44+00:00 app[web.1]: app/controllers/answer_selections_controller.rb:10:in `create'
同样,我在另一个表上看到了同样的错误:
2012-09-09T04:10:34+00:00 app[web.2]: ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "trips_pkey"
2012-09-09T04:10:34+00:00 app[web.2]: : INSERT INTO "trips" ("booked", "created_at", "destination_id", "end_date", "individual_id", "n_travelers", "name", "start_date", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"):
由于连接超时,无法直接访问 postgres 数据库:
$ heroku pg:psql
psql: could not connect to server: Connection timed out
我该如何修复它,以及将来如何避免它?我已经读过运行 VACCUUM 和 ANALYZE 会解决它,但正如我所说我无法访问 psql。
编辑以添加请求的信息(上面包含的示例架构,我们正在使用 ActiveRecord):
@record.create(params) # How these records are created