chrisdhanaraj
03/28/2021, 2:10 AMconnect
is just an abstraction? (https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries/#connect-an-existing-record)?
i.e., I let's say I have
modal Author {
id
posts. Post[]
}
modal Post {
id
authorId. Int
author. Author. @relation(fields: whatever)
}
Let's say I create a new Author and want to swap some posts to them. Is there a functional difference between
prisma.author.create({
data: {},
posts: connect{}
}
vs
prisma.author.create({ data}
prisma.posts.updateMany({
where: {
authorId
},
data: {}
}
chrisdhanaraj
03/28/2021, 2:33 AMprisma.posts.update({
where: {
id
},
data: {
connect: {
author: {
id: authorId
}
}
}
}
Ryan
03/29/2021, 7:16 AMconnect
lets you create the record and its relation in the same call and Prisma internally handles this in a transaction so the creation of the record and the relation will be atomic.
The first example is recommended as in the second one, you would need to handle the rollback logic yourself in case there’s an error.
If I update with a new connect, that does that automatically disconnect the previous connection?If it’s a many-1 relation then yes. In this case, a Post can have only 1 author so yes the author will be replaced. If it’s a 1-many relation, then no, connect will add a new relation to the entity. If you were doing the opposite i.e. connecting a postId to the author, then it wouldn’t replace it.
chrisdhanaraj
03/29/2021, 12:58 PMRyan
03/29/2021, 1:18 PMPrismaClient
instance so that you can get the logs for all the queries that you run.chrisdhanaraj
03/29/2021, 6:10 PM