bobmanary

bobmanary

Distant relations, deleting, and getting database constraint errors into a changeset

I have a Postgres database with these tables and associations:
A has many B, B has zero or one C, C has many D.

I have a form that manages a single A record and its child B records. When the user hits save, I want to prevent deleting a B record if it has any instances of D through table C. This is working properly with constraints at the database level:

delete from B where id = 123;
ERROR:  update or delete on table "C" violates foreign key constraint "D_C_id_fkey" on table "D"
DETAIL:  Key (id)=(456) is still referenced from table "D".

However, I can’t get the database error to propagate back to the A/B changesets and I instead get an exception. Is it possible with no_assoc_constraint (or anything else) to get that? Which module should the changeset constraint be applied to if the form only concerns itself with A/B and doesn’t preload or otherwise know about C/D?

I’ve tried putting has_many :d, through: [:c, :d] plus changeset |> no_assoc_constraint(:d) (with and without the constraint name option) in the B schema and that results in " cannot add constraint to changeset because association D does not exist."

First Post!

garrison

garrison

This is a guess (that is to say I haven’t tested it), but I believe if you use foreign_key_constraint/3 with the name option set to the constraint which is actually violated it would apply the error to the field you choose.

You could do the same with no_assoc_constraint/3, it just uses an assoc name instead of the actual foreign key field.

Where Next?

Popular in Questions Top

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
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
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement