albiere
Validation with `Ecto.Changeset.unsafe_validate_unique` and `Ecto.Changeset.put_assoc`
Hey peeps. Has anybody used Ecto.Changeset.unsafe_validate_unique with an association? I’m trying to do something like this:
organization = Repo.get!(Organization, 1)
%User{}
|> Ecto.Changeset.cast(%{email: "email", pass: "pass"}, [:email, :pass])
|> Ecto.Changeset.put_assoc(:organization, organization)
|> Ecto.Changeset.unsafe_validate_unique([:email, :org_id], MyApp.Repo)
I read the implementation of unsafe_validate_unique and the query doesn’t know how to handle associations. It only adds a simple where clause for chosen fields. Here’s the link to the implementation: ecto/lib/ecto/changeset.ex at 599a1a327b78320f85551c62b14fb7d272439014 · elixir-ecto/ecto · GitHub
In my case, I don’t want to cast the org_id, since that’s not something that should come directly from the user. Has anybody run into something similar? I feel like that would be a very common scenario for any type of SaaS type of app.
First Post!
albiere
I’m not sure if there’s a reason not to support changes that are associations, but would a change in the current implementation make sense?
Link to current implementation: ecto/lib/ecto/changeset.ex at 599a1a327b78320f85551c62b14fb7d272439014 · elixir-ecto/ecto · GitHub
where_clause = for field <- fields do
case changeset.types[field] do
{:assoc, %Ecto.Association.BelongsTo{} = assoc} ->
assoc_struct = get_field(changeset, field)
{assoc.owner_key, Map.get(assoc_struct, assoc.related_key)}
_ ->
{field, get_field(changeset, field)}
end
end
And here’s the change that could potentially be implemented. What are your thoughts?
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









