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

Usage question #17

Open
mikepc opened this issue Sep 11, 2023 · 1 comment
Open

Usage question #17

mikepc opened this issue Sep 11, 2023 · 1 comment

Comments

@mikepc
Copy link

mikepc commented Sep 11, 2023

Since the underlying data type in postgres is uuid DB clients render the ULID as a UUID. I can query using the DB client fine with these IDs, but how do I query for records from within iex

eg. iex(2)> MyApp.Repo.get!(Blog, "<TBD>") I tried a couple of encoding/decoding ideas, but I'm drawing a blank.

@voughtdq
Copy link

If you're talking about a UUID received from a non-Elixir DB client, the way to go about this is to use Ecto.UUID.dump/1 first. So, given a UUID representation of a ULID 018f7ad0-d3da-6a9c-61de-b883f459a3a1:

iex(1)> {:ok, uuid_bin} = Ecto.UUID.dump("018f7ad0-d3da-6a9c-61de-b883f459a3a1")
{:ok,
 <<1, 143, 122, 208, 211, 218, 106, 156, 97, 222, 184, 131, 244, 89, 163, 161>>}
iex(2)> {:ok, ulid} = Ecto.ULID.load(uuid_bin)
{:ok, "01HXXD1MYTDAE63QNRGFT5K8X1"}
iex(3)> MyApp.Repo.get!(Blog, ulid)
%Blog{...}

If you're going to be doing this a lot, it might make sense to fork this project and customize Ecto.ULID.cast/1 so it also accepts and validates a ULID-conformant UUID.

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

No branches or pull requests

2 participants