2

I am using prisma + mysql (on planetscale). When I link two items that are in different tables, I normally use connect or disconnect:

const getUser = await prisma.user.update({
  where: {
    id: 9
  },
  data: {
    posts: {
|      connect: {
|        id: 11
|      },
      create: {
        title: "My new post title"
      }
    }
  }
})

I am wondering whether that's necessary or why that's necessary?

I also noticed that I can just update records in my database by updating the id (as a plain string), and it will still work. e.g.:

// example for updating a one-to-many relationship:
const getUser = await prisma.user.update({
  where: {
    id: 9
  },
  data: {
    postId: "123192312i39123123"
    }
  }
})

... or if it's an explicit many-to-many relation, I can just edit the row in the relation-table & update the id.

Is this a bad way of doing things? Am I going to break something later down the line in doing it this way?

antonwilhelm
  • 5,768
  • 4
  • 19
  • 45

1 Answers1

0

Your cloud provider is not relevant in the context of the question. It will not affect how your framework(prisma) behaves in updates.

I am wondering whether that's necessary or why that's necessary?

You have a user with a one to many relation: user => n posts.
You have an existing post in the db, and you want to add that post to the posts collection of a user.

That posts relation can be either explicit or implicit. The connect clause handles the addition of relation:

{
  posts: {
    connect: { id: 11 }
  }
}

Without using the connect you'd have to create a new post:

{
  posts: {
    create: {
      title: "My new post title"
    }
  }
}

update records in my database by updating the id (as a plain string)

Not sure what you mean here, mind sharing the schema?

or if it's an explicit many-to-many relation, I can just edit the row in the relation-table & update the id

If it's explicit many-to-many then it's OK to manually edit the id fields. As long as the ids are found and the relation makes sense, there's no problem with manual updates.

EcksDy
  • 1,289
  • 9
  • 25