Skip to content
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

Support for UUID primary keys? #14

Closed
rusterholz opened this issue Mar 23, 2017 · 1 comment
Closed

Support for UUID primary keys? #14

rusterholz opened this issue Mar 23, 2017 · 1 comment

Comments

@rusterholz
Copy link

I'm hoping to use PaperTrail in a Phoenix project where all primary keys on database tables are UUIDs instead of integers.

Adapting the add_versions migration generated by PaperTrail to use UUIDs is fairly straightforward:

  def change do
    create table(:versions, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :event,        :string, null: false, size: 10
      add :item_type,    :string, null: false
      add :item_id,      :binary_id
      add :item_changes, :map, null: false
      add :originator_id, references(:users, type: :binary_id)
      add :origin,       :string, size: 50
      add :meta,         :map

      add :inserted_at,  :utc_datetime, null: false
    end

However, as you might expect, actually attempting to insert/update a version fails because the item_id and originator_id in the version schema are still set to :integer:

iex(4)> PaperTrail.insert(changeset)
** (Ecto.ChangeError) value `"070e8aba-85bd-4375-993d-0bdcc1ceb76f"` for `PaperTrail.Version.item_id` in `insert` does not match type :integer
             (ecto) lib/ecto/repo/schema.ex:699: Ecto.Repo.Schema.dump_field!/6
             (ecto) lib/ecto/repo/schema.ex:708: anonymous fn/6 in Ecto.Repo.Schema.dump_fields!/5
           (stdlib) lists.erl:1263: :lists.foldl/3
             (ecto) lib/ecto/repo/schema.ex:706: Ecto.Repo.Schema.dump_fields!/5
             (ecto) lib/ecto/repo/schema.ex:655: Ecto.Repo.Schema.dump_changes!/6
             (ecto) lib/ecto/repo/schema.ex:200: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
             (ecto) lib/ecto/multi.ex:406: Ecto.Multi.apply_operation/5
           (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
             (ecto) lib/ecto/multi.ex:396: anonymous fn/5 in Ecto.Multi.apply_operations/5
             (ecto) lib/ecto/adapters/sql.ex:615: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
    (db_connection) lib/db_connection.ex:1274: DBConnection.transaction_run/4
    (db_connection) lib/db_connection.ex:1198: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:789: DBConnection.transaction/3
             (ecto) lib/ecto/repo/queryable.ex:21: Ecto.Repo.Queryable.transaction/4
                    lib/paper_trail.ex:91: PaperTrail.insert/2
iex(4)>

I would hope to be able to set the primary key type as a top-level configuration value to :paper_trail, e.g. something like this in config.exs:

config :paper_trail,
  repo: MyProject.Repo,
  key_type: :binary_id

I am new to Elixir, and less-than-confident in my ability to write a solid patch for this, but I'm happy to take a stab at it if you don't have the bandwidth or interest in working on it yourself.

@izelnakri
Copy link
Owner

izelnakri commented Mar 23, 2017

Hi Andy, thanks for reporting this interesting issue. I would say that current behavior is intentional. Error is related to the PaperTrail.Version Ecto model type for :item_id.

I've designed PaperTrail for versioning many types of models at once, you can try to implement UUID versioning however then all your tracked models primary keys should be UUIDs. This makes the scope of this feature very tiny. Feel free to take a stab at it, I will review and share my opinion, the worst case you can create your own fork of paper_trail and publish it as an individual package.

I've never used UUIDs as primary keys, never really had a good use case for it. PaperTrail actually uses Postgres Sequences for the version inserts on strict mode thus you will probably have to rewrite that part as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants