Closed EleanorRagone closed 9 years ago
I am not familiar with the error unfortunately. What is the 'versions_pkey'
constraint they are referring to? Or is that PG's way of referring to a primary key for the versions
table? I don't think the versions
table would have a primary key by default if you used the base setup from the generator.. just an index. So maybe it's an index duplicate insert, but it shouldn't be a unique index by default so I'm not sure why that would occur.
Closing for now, but feel free to follow up if you encounter these issues again.
Hi, I am also getting this error when I save a previous version in order to rollback.
Thanks, Tom.
What version of PaperTrail are you using?
I get a similar error. paper_trail (4.0.0.beta2) tries to save my entry while the old one still exists, when I call PaperTrail::Version.find(params[:id]).reify.save!
:
INSERT INTO "reservations" ("id", "created_at", "guest_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" ["id", 15], ["created_at", "2015-03-06 16:54:04.016854"], ["guest_id", 6], ["updated_at", "2015-03-09 10:57:10.457005"]]
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "reservations_pkey" DETAIL: Key (id)=(15) already exists.
Looks like these are issues of your database table sequence falling out of sync or something. This StackOverflow post might give you some help on how to reset the sequence on your table.
Although I do find it a bit strange that it's attempting to do an insert
instead of an update
on this row, so maybe that's what's going wrong here. When I try this on my local w/ SQLite I get an update statement:
2.0.0-p247 :004 > PaperTrail::Version.find(2).reify.save!
PaperTrail::Version Load (0.2ms) SELECT "versions".* FROM "versions" WHERE "versions"."id" = ? LIMIT 1 [["id", 2]]
Widget Load (0.1ms) SELECT "widgets".* FROM "widgets" WHERE "widgets"."id" = ? LIMIT 1 [["id", 1]]
(0.0ms) SAVEPOINT active_record_1
SQL (0.0ms) UPDATE "widgets" SET "name" = ?, "updated_at" = ? WHERE "widgets"."id" = 1 [["name", "foobar"], ["updated_at", "2015-03-09 16:03:21.239462"]]
SQL (0.1ms) INSERT INTO "versions" ("created_at", "event", "item_id", "item_type", "object", "object_changes", "transaction_id") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", "2015-03-09 16:03:21.239462"], ["event", "update"], ["item_id", 1], ["item_type", "Widget"], ["object", "---\nid: 1\nname: bar\na_text: \nan_integer: \na_float: \na_decimal: \na_datetime: \na_time: \na_date: \na_boolean: \nsacrificial_column: \ntype: \ncreated_at: 2015-03-09 16:03:06.105316000 Z\nupdated_at: 2015-03-09 16:03:15.704597000 Z\n"], ["object_changes", "---\nname:\n- bar\n- foobar\nupdated_at:\n- 2015-03-09 16:03:15.704597000 Z\n- 2015-03-09 16:03:21.239462000 Z\n"], ["transaction_id", 1]]
(0.0ms) RELEASE SAVEPOINT active_record_1
=> true
So perhaps it's a Postgres / ActiveRecord error. Does this happen for you w/ PaperTrail 3.x and also what version of ActiveRecord are you using?
I get the same error with version 3.0.7 (ActiveRecord 4.2.0), but I have an idea why PaperTrail tries to insert
the row. The entry I want to reset is excluded by a default_scope. ActiveRecord might think the entry was deleted and has to be inserted...
Update:
I don't get an error when I remove the default_scope
.
I looked more closely at the gem's code:
version_concern.rb:8
uses a standard belongs_to
association:
belongs_to :item, :polymorphic => true
Thereby any default_scope
which is defined in the item's model will be applied. I think it would be an improvement if PaperTrail would use an unscoped query. Otherwise any item "hidden" by default_scope
will cause an error.
@rvfx - can you try using the :dup => true
option arg to the reify
method? like so:
PaperTrail::Version.find(params[:id]).reify(dup: true).save!
No changes with :dup => true
. :(
Ok what is your suggested adjustment that you think would fix the issue? Drop this approach and do something more along the lines of this?
inheritance_column_name = item_type.constantize.inheritance_column
class_name = attrs[inheritance_column_name].blank? ? item_type : attrs[inheritance_column_name]
klass = class_name.constantize
model = klass.unscoped.find_by_id(item_id) || klass.new
Any chance you can provide a failing test case via a PR or something?
The unscoped version works great! PR coming up...
@rvfx - Did you ever find time to implement this code into something that could be made into a PR? I'm trying to tie of loose ends for 4.x before an official release and I think this would be a good addition, but I'm unsure of exactly what should change since I don't have test cases to duplicate this issue..
The code you suggested works fine. Sorry, I haven't found time to make a commit.
For what it's worth, I was seeing this issue with a custom subclass in development mode. Rails 6.0.3.6, PaperTrail 12.0.0. I had this class:
class SubjectVersion < PaperTrail::Version
self.table_name = :subject_versions
end
When I would reset the database with our seeds, there would be 16 records in the subjects_versions
table, but for some reason Postgres would have the next id as 2. I instead switched it to include VersionConcern
, and things seem to work better at first glance. The next id is 17 and the page that was throwing the error allows me to continue:
class SubjectVersion < ApplicationRecord
include PaperTrail::VersionConcern
end
Hi there -
I'm having intermittent problems that I'm having trouble replicating, but I figured I'd reach out to you guys and see if you're familiar with it.
Long story short, this is the error:
The conditions:
It's a big problem because this error ends up rolling back my update:
I re-issue the save command, and everything works fine.
Are you familiar with this condition happening? Can you give me any more insight? I'm happy to try and replicate this more and identify it, but, like I said, it seems to be an intermittent problem.
Edit: Looking at this some more, it seems to be issuing a double-insert into versions; I'm not sure why, when there's clearly only a single update to the users table.