Prisma data modeling has many and belongs to

被刻印的时光 ゝ 提交于 2019-12-02 01:57:45

As per the documentation, the @relation directive is used to specify both ends of a relation.

Let's take the following datamodel:

type User {
  postsWritten: [Post!]!
  postsLiked: [Post!]!
}

type Post {
  author: User!
  likes: [User!]!
}

Here, we have an ambiguous relation between Post and User. Prisma needs to know which User field (postsWritten? postsLiked?) to link to which Post field (author? likes?)

To resolve this, we use the @relation with a name used in both ends of the relation.

This would make the datamodel look like this:

type User {
  postsWritten: [Post!]! @relation(name: "AuthorPosts")
  postsLiked: [Post!]! @relation(name: "UserLikes")
}

type Post {
  author: User! @relation(name: "AuthorPosts")
  likes: [User!]! @relation(name: "UserLikes")
}

Because we used the same name for the postsWritten and author fields, Prisma is now able to link these two in the database. Same for postsLiked and likes.

In conclusion, the problem with your datamodel is that you used different names in your relation. This confuses Prisma which think those are different relations. Which explains why you can query one way but not another.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!