sirfitz

sirfitz

Ecto foreign_key_constraint throwing error even though it's in the changesset

Hi Everyone :slight_smile:

I’m having a bit of trouble with has_many foreign key relationships in Ecto, I’ve followed the instructions but with no luck. I’ve added the foreign_key_constraint to the changeset, so I’m expecting an {:error, _} tuple, but i’m getting an uncaught Ecto.ConstraintError

My migration:

My Post Changeset

The Error:

Any help would be appreciated in resolving this, thanks :smiley:

Most Liked

sirfitz

sirfitz

Found the Solution:

TLDR: Pipe your struct into a changeset first haha :smiley:

Incorrect:
post |> Api.Org.Repo.delete()

Correct:
post |> Api.Assets.Post.changeset(%{}) |> Api.Org.Repo.delete()

Explanation
Ecto’s constraints_to_errors function uses the changeset to detect the constraints, however because I piped my struct directly into the Repo.delete function instead of passing it to a changeset first, the constraint I set was never picked up

Screenshot From Ecto:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Glad you sorted it out!

As a minor note, copying and pasting text works a lot better than screen shots. It allows others who want to help to copy some part of your text and edit it easily to improve it without having to retype all of it by hand. It also works better on different resolution / size screens.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement