我有一个包含用户数据的User表。我还有一个Relationship表,其中包含一个parent_id和一个child_id以及递增的id。每个父对象可以有多个子对象,我正在尝试使用parent_id检索子对象的数组。我目前正在获取一个数组,但该数组只包含Relationship表数据。
如何从User表中获取数据?我的印象是,因为有一种关系,下面的应该是有效的,但它不是。
query {
getChildren {
user{
id
name
email
}
}
}相关代码如下:
查询:
export const getChildren = queryField('getChildren', {
type: 'User',
list: true,
resolve: async (_parent, {}, ctx) => {
const userId = getUserId(ctx);
if (!userId) {
// TODO -think I might need to throw an error here
return;
}
const children = await ctx.prisma.relationship.findMany({
where: {
parent_id: userId,
},
});
return children;
},
});schema.prisma:
model Relationship {
child_id Int
id Int @default(autoincrement()) @id
parent_id Int
child User @relation("Relationship_child_idToUser", fields: [child_id], references: [id])
parent User @relation("Relationship_parent_idToUser", fields: [parent_id], references: [id])
}
model User {
created_at DateTime @default(now())
email String? @unique
id Int @default(autoincrement()) @id
ischild Boolean @default(false)
name String?
password String
children Relationship[] @relation("Relationship_child_idToUser")
parents Relationship[] @relation("Relationship_parent_idToUser")
}发布于 2020-05-26 18:19:28
下面的查询应该可以完成此操作
prisma.relationship.findMany({
where: {
parent_id: user_id,
},
include: {
child: true,
},
})您需要显式指定include参数才能在输出中包含该关系。
https://stackoverflow.com/questions/62018514
复制相似问题