![]() ![]() The bottom line is: just because a changeset is valid does not mean that an insert will succeed. In this post, I would like to present how you can use Ecto to check any information from the user. When the insert is actually executed, then the db applies the check constraint, which causes the insert to fail, and ecto logs a QUERY ERROR. errors tells you if there are any detected errors in the changesets fields (a wrongly formatted username for example). The use of Ecto.Changeset is practically a standard because we have a unified method of checking parameters and handling errors. The difference in output indicates that only after all validations pass does Ecto attempt to execute the insert. In the latter output, note: QUERY ERROR db=6.1ms queue=1.5ms INSERT INTO "reviews" ("contents","stars","title","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id", ~N] Phoenix/Ecto - changeset error when updating embedsmany record Asked 5 years, 10 months ago Modified 5 years, 10 months ago Viewed 962 times 0 In my Phoenix app, I am getting a no function clause matching in /2 error when trying to update a model with an embedsmany relationship. Not sure how the atom/String handling works when inserting and retrieving from the DB.(.) Now, when invoking Repo.insert/2 or Repo.update/2, if the price is not positive, it will be converted into an error and ) law is it officially argued that the U.S. Using get_field instead of get_changewill try to get the field from the changes, but if it wasn't changed, it can be taken from the existing struct and the rest of your function should work normally 0 (ArgumentError) could not generate inputs for Hot Network Questions Where in U.S.![]() One workaround could be to update the function like: defp validate_expires_at(changeset) do If I understand your problem correctly, I think that to solve it you should probably consider the struct given to the changeset as well.īecause, the way your code is, you're only checking for the state from the changes in the changeset, but if you try to update the expires_at alone, the changes in the changeset will not include the state that might already be set to "draft", and therefore, the cond block in your validate_expires_at function will always match true, because the value will be nil. You need to pass the Ecto.Changeset returned by Shop.changeset/2 to Repo.insert and not a Shop struct directly if you want the validations and uniqueconstraint defined in that function to have any effect. Where it's being called: def create_document(attrs \\ %) do I really appreciate that gettext works out of the box. _ -> add_error(changeset, :expires_at, "cannot be set when state is not draft")īut it doesn't really work as I can update the expires_at to nil even if the state is draft. Hello, I am working on a Phoenix app that needs to work with a few different languages. Get_change(changeset, :state) = :active -> foldl / 3 Expected behavior Embeds stay wrapped in changeset structs, so that traverseerrors works on them too. ex: 2365: anonymous fn / 4 in Ecto.Changeset. Nil -> add_error(changeset, :expires_at, "can't be blank when state is draft") traverseerrors / 2 (ecto) lib / ecto / changeset. Get_change(changeset, :state) = :draft -> I tried the following: defp validate_expires_at(changeset) doĮxpires_at = get_change(changeset, :expires_at) ![]() The validation I'm trying to implement is: The expires_at can only be set if the state is draft (this should also be valid for updates, I should not be able to remove the expires_at if the state is still draft) Therefore changeset.valid could never perform a database operation, it is just data reflecting a set of. function /3 is undefined (module Ecto. My schema is: defenum(VersionStateEnum, ) Remember, there are no objects in Elixir nor in Ecto. I'm having a bit of trouble trying to implement a custom changeset validation. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |