Search code examples
prisma

Advantage of using connect over updating foreign keys directly


Why use connect?

data:{
  'userId': 1
}

the above one is not enough??

Why use

user:{
 connect:{
    id: 1
  }
}

Isn't the result the same? I wonder


Solution

  • To answer your question, why connect exists at all:

    connect (and disconnect) provide an alternative interface to relations that you can also achieve by updating the respective fields directly. However, there are many cases, when the API is much more convenient.

    E.g.

    • updating the object that does not store the attribute that represents the relation
    • updating a many-to-many relation
    • more advanced interfaces like connectOrCreate
    • connecting entities on unique attributes other than the primary key