:wave: I'm trying to learn some Prisma concepts, a...
# orm-help
c
👋 I'm trying to learn some Prisma concepts, and just curious if
connect
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
Copy code
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
Copy code
prisma.author.create({
  data: {},
  posts: connect{}
}
vs
Copy code
prisma.author.create({ data}
prisma.posts.updateMany({
  where: {
    authorId
  }, 
  data: {}
}
Maybe another salient point - if I update with a new connect, that does that automatically disconnect the previous connection? i.e.
Copy code
prisma.posts.update({
  where: {
    id
  },
  data: {
    connect: {
      author: {
        id: authorId
      }
    }
  }
}
r
@chrisdhanaraj 👋
connect
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.
c
Gotcha, thank you! That makes sense - I think what would've helped me in the documentation is the equivalent SQL that Prisma is generating for the command; right now y'all display the model which is helpful, but having the SQL statements that it all maps to would make it pretty explicit
r
You can configure logging when creating the
PrismaClient
instance so that you can get the logs for all the queries that you run.
c
Yep, I just wanted to understand the transaction before I executed it 😄 Just a personal preference on how I learn more than anything else