Help migrating a nested Prisma update
Been trying to set this up without going through the raw sql route for some time but cant seem to wrap my head around it. Basically the structure here is I have a Result that can optionally have an alert or a note. If an alert or note already exist then update them. Any help here is appreciated it! thank you
7 Replies
Does note have a
result_id
foreign key?
Result and note have a one to one relation?Result has a noteId foreign key
It’s an optional relation
So basically each result can optionally have one note associated with it. But a note does not have to be associated with an a result. It can be used for non result scenarios
Well, Prisma hides away all the real necessary steps to makes this work so much, that you end up learning prisma, not sql
You can't write to 3 different tables at the same time in sql, so you'll need to do the individual queries inside a transaction
That will do the trick but it won't return back anything. If you want the data, you can either use the
returning()
method on each query and then construct your object, or do a SELECT
and the end and return it inside the transactionlogically that seems to break it down well but I get a following error when I run it I'm guessing it has to do with the constraint: "there is no unique or exclusion constraint matching the ON CONFLICT specification"
Yeah, the parentId needs to be a unique index in note. Is it not?
We only have the unique index in the result not the note. Not sure if that needs to be corrected
It's up to your db architecture. But in that case, you might just need to run a select just to check if the note with that patientId exists, update if it does, insert if it doesn't