wolfiton
Best way to generate an UUID for the id field?
Hello,
I am having a hard time to find the proper way to implement the UUID method on the field in my schema and my migrations.
So far I found one article on medium https://medium.com/@brucepomeroy/specifying-primary-key-values-with-ecto-57405b7ecbc5 and the doceumntation Ecto.UUID — Ecto v3.14.0.
The example that the medium article offers seems outdated and it looks like this:
@doc""" Generated migration for companies with the id field using UUID 4"""
defmodule MyApp.MyMigration do
use Ecto.Migration
def change do
create table(:companies, primary_key: false) do
add :id, :uuid, primary_key: true
add :title, :string
timestamps()
end
end
end
Then the schema looks like this
@doc """ Generated schema for company with autogenerated uuid"""
defmodule MyApp.Company do
use Ecto.Schema
@primary_key {:id, :binary_id, autogenerate: true}
@derive {Phoenix.Param, key: :id}
schema "companies" do
field :title, :string
timestamps()
end
end
My question is does this work with absinthe and is UUID 4 secure enough?
Wouldn’t be better to use a more powerful uuid, composed from letters and numbers, is there a package that does this?
Thanks in advance
Marked As Solved
kokolegorille
If You have an authorization system in place, then putting some random id should not affect the security of your system.
User identity can be managed by sessions, cookies, or tokens in case of API.
Also Liked
kokolegorille
I don’t see any reason why it would not work with Absinthe. If You use node interface, You will see that GraphQL has a very simple way to generate unique node id.
Secure enough? I don’t think it is used for security reason, but to ensure uniqueness. Unless guessing the id leads to security issues.
I have also used KSUID with Ecto without problem. It does add time order to keys.
There is an example of KSUID in Mastering Elixir (Self-published / Packt)
wolfiton
If You have an authorization system in place
You have a valid point there, that is why I am struggling to create one and make it open source to help others as well.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









