prisma transaction api
hi, so I figured I need to use the transaction api but I cant seem to get the syntax right.
heres what im trying to do:
I have a board model and column model, a board can have many columns.
When creating a board, I'd want the user to also be able to create columns, so the problem is getting the created board's id to create the columns (need a ref to the board) and so I read about the transaction api but I keep running into problems and I am probably getting the syntax wrong.
Would love some help!
29 Replies
That should be enough to go with but you can create the board first and use the id returned to create your columns
Thank you very much for the reply!
I forgot to mention but if I would want to create more than one column, is it okay to use map like this?
That columns return is gonna be a Promise
im wondering if columns wouldnt be an array of promises and if so, do i need to do anyhting?
There might be a better way to create from an array but let me google this one sec
With promise array you can read up on Promise.All() which acts as a "barrier" if you've done some multi processor stuff. If not it'll basically wait for it all to finish before continuing
its a way to avoid waterfalls if you have non dependent async work
Can you show me your schemas for these 2 models
@beylon
I just want to see the relation so you can pull out all the other stuff if its sensitive
its very basic
You may not even need transactions for this, have you tried just using nested writes? Prisma will manage the transaction stuff for you
I tried but I would need the board's id to create a column so its not possible
That’s the point - Prisma should automatically insert the board’s id when u create the columns
Stack Overflow
Create or update one to many relationship in Prisma
I'm trying to update a one to many relationship in Prisma. My schema looks like this
model A_User {
id Int @id
username String
age Int
bio Strin...
yeah check this out
That’s the point of it being nested, the foreign keys are managed for you
Like so?
Ya like that
It gave me an error saying boardId is undefined
I think it’s bc you’re providing userId as well
You need to use
user: { connect: … }
insteadis this instead of inserting the userId for the board?
Yeah
Prisma usually requires that you use all-foreign-keys or all-nested, not both
wait, how do I do that? i thought its {connect:{boardId:id}}
Nah just do it for the user
connect: { id: ctx.session.user.id }
It doesn't work
Oh soz user: { connect: …
This works! thank you so much!
It created a board and cols that have a ref to that board! tyyyyyyy!!
Glad i could help!
weirdly enough, this works too
also, found this in prisma docs
Wtf I have no idea