I have a prisma data model that consists of a root Category and a Subcategory. A Category has many Subcategories and a Subcategory belongs to one Category. My model looks
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.