grangerelixx

grangerelixx

Cast_embed followed by unique_constraint doesn't detect uniqueness errors

Hi, I have an ecto schema whose changeset function is in the following structure

def changeset(post, attrs) do
    post
    |> cast(attrs, @fields)
    |> cast_embed(:blog)
    |> unique_constraint([:name])
    |> validate_required(@fields)
  end

When there is a duplicate name and also some error in blog cast_embed, it should return errors from cast_embed and unique_constraint error but it only returns cast_embed error. How to get all errors?

First Post!

joey_the_snake

joey_the_snake

It shouldn’t and here’s why:

The unique constraint can’t be validated unless the command is sent to the database. Your application won’t know if there’s a duplicate entry in the database table without doing so. If there’s an error with anything else the command won’t be sent to the database because there is a problem and it shouldn’t be attempted.

Most Liked

trisolaran

trisolaran

As @joey_the_snake mentioned, unique_constraint/3 only adds the error if your changest hits the database. You can however add an unsafe_validate_unique/4 to optimistically check for duplicate entries before insertion and add an error to the changeset if a duplicate is found. This is useful to show the user early on that the operation can’t succeed (and will probably catch most of the cases, the rest will be caught by unique_constraint/3)

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement