albiere

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

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?

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

We're in Beta

About us Mission Statement