-
Notifications
You must be signed in to change notification settings - Fork 884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
t.integer :item_id does not play well with, nor complains about id: :uuid #363
Comments
If you want to make a pull request, then by all means, this seems like a logical idea. I will say, that to me (perhaps I'm biased as I am a maintainer of the gem who knows the internals pretty well), it would seem somewhat obvious that you can't have the |
Agreed. We should throw an exception and not comply. On Apr 29, 2014, at 2:22, Ben Atkins notifications@github.com wrote: If you want to make a pull request, then by all means, this seems like a Reply to this email directly or view it on |
@evadne - Have you taken any cracks at adding an informative error message? |
Not yet, I got sidetracked. On May 24, 2014, at 1:20 AM, Ben Atkins notifications@github.com wrote:
|
@evadne - Any progress on this? Trying to get this into |
A function to inspect the data type of the primary key could be as simple as: def paper_trail_assert_supported
if columns_hash["id"].try(:type) != :integer
fail ::PaperTrail::UnsupportedModel,
"PaperTrail only supports models with integer primary keys."
end
end I see two possible times such an assertion could be made:
|
+1 on addressing this—at least with an exception if not an overall change that supports UUIDs. Current state is that everything appears to work fine, but once you have enough records you'll start seeing odd The reason for the problem is pretty obvious once you investigate and realize FWIW - the current approach keys off of a composite index of There are a variety of advantages to using UUID keys; I hope support will be considered for a future version of paper_trail. |
Let's start with the former, ie. an exception. See my notes on a potential implementation. A PR would be welcome, thanks! |
I recently got bit by this and had to change the column item_id column type from an integer to a string. I would have preferred sticking with the native UUID type, though was unless if that would be dangerous. |
@preston you can also change the migration file to uuid type |
@h8rry Tried it, but as the OP describes in detail, it's problematic. |
@batter. Can you confirm that if the following would work reliably? It works for me in Rails 5. If you are using Postgres, you can change the item_id to def change
create_table :versions, versions_table_options do |t|
t.string :item_type, item_type_options
t.uuid :item_id, null: false
t.string :event, null: false
...
end
add_index :versions, [:item_type, :item_id]
end
... Also, if you want the each version record to have an id with def versions_table_options
if mysql?
{ options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci", id: :uuid }
else
{ id: :uuid }
end
end |
Thanks for the suggestion @h8rry, neat to hear you have this setup working.
I can't, but we can certainly try to work it into our test suite as a special custom version class and see if it works for ActiveRecord 3 & 4 as well.
It looks semi-reasonable to me, however, are there other SQL databases beyond Postgres that support a |
Just tested @h8rry solution with Postgres. Confirm that it works well for me. |
As Ben says, the next step on this issue is to add tests. Start by creating a new model in |
Just to be clear, is @h8rry's solution the "official" (suggested) way of using PT with I personally think that it works, but I don't know what would happen if not all of the tables of the models that |
We can't "officially" support something until we have tests for it. |
Closing due to inactivity. Contributions welcome, as described above. |
We just ran into this issue as we have a mix of Question - instead of making the |
wtf why is this closed its very much an issue |
Making the column a Be aware, when altering tables, DBs typically lock the table and then cast the data. |
Silent data loss occurs for versions generated against models that use
id: :uuid
if the backingitem_id
column is not also an:uuid
. Versions are created and retrieved by a meaningless portion of the model’s primary key.This following configuration:
create_table :users, id: :uuid { |t| …
create_table :versions, { |t| … t.integer :item_id, :null => false
Exposes these following issues:
item_id
(obviously)has_paper_trail
.User#versions
returns both versions.This is caused because:
#versions
was able to retrieve both version modelsI propose hardening the project by:
has_paper_trail
and relevant initializers, so preliminary checks are carried out against the schema of the source table and the target versions table. If there is any inconsistency between the primary key column type on the model and the item_id column type on the versions table, throw an exception and do not proceed.#versions
finder, so we know why a version model with an incorrectitem_id
was returned, and are able to then fix other relevant issues if needed.I am aware that this is caused by misconfiguration and is not a bug. I am also able to completely mitigate the problem by ensuring type consistency between the model’s identifier column and the item_id column in the versions table. However, I think it is highly beneficial for Paper Trail to identify and expose this kind of misconfiguration intelligently.
I am not aware of such a fix on
master
and volunteer to fix it, as long as this addition is considered relevant and appropriate. Please close this issue if it is out of scope or inappropriate.Thanks so much.
Environment:
The text was updated successfully, but these errors were encountered: