Is there any substantial difference in using `conn...
# prisma-client
d
Is there any substantial difference in using
connect
or not when creating a row with a foreign key field? I note the result is the same but what would I choose one over the another one? Or this just for a declarative/semantical reason? Example:
Copy code
model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  rooms         Room[]
}


model Room {
  id          Int    @id @default(autoincrement())
  userId      String
  title       String
  description String @db.Text
  user        User   @relation(fields: [userId], references: [id])

  @@unique([userId, title])
}
From the above schema, If I would like to add a new room I could do: Let's say
user
hs already being resolved to a user found in the User model
Copy code
prisma.room.create({  data: { title, description, user: { connect: { id: user!.id } } }})
or also,
Copy code
prisma.room.create({  data: { title, description, userId: user!.id }} )
What is the correct way?
n
Hey Demian 👋 You are correct, you could directly set the foreign key and use connect as well. Both should work. However, it is better to use connect to link a foreign key because prisma would throw an error in case it cannot find the connecting record, so connect would provide an extra layer of validation.
🙌 1