sloanelybutsurely / typeid-elixir

Elixir implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
MIT License
58 stars 8 forks source link

Improvements for Ecto #25

Closed andrewtimberlake closed 2 months ago

andrewtimberlake commented 1 year ago

I was trying to migrate from bigint ids to TypeID I came across a few problems because I need to have TypeID working during interim steps.

  1. You can’t use TypeID as a standard, non primary-key field (this would fail to pick up the prefix)

    schema "table" do
    field :uuid, TypeID, prefix: "user", type: :binary_id
    end
  2. You can’t set the foreign key type aside from using @foreign_key_type TypeID

    belongs_to :model, Model, type: TypeID

This pull request addresses both usages I didn’t see any Ecto tests, so I haven’t provided tests.

sloanelybutsurely commented 1 year ago

hi! thanks for taking the time to contribute. i'll need a moment to review this more thoroughly but i did have some immediate feedback for you:

2. You can’t set the foreign key type aside from using @foreign_key_type TypeID

you can do this using define_field: false on the belongs_to and defining the foreign key field manually. See https://github.com/sloanelybutsurely/typeid-elixir/issues/20#issuecomment-1635829743 for a more complete example.