Toru Eguchi
01/04/2022, 10:19 AMmodel User {
id Int @id @default(autoincrement())
name String?
children FamilyRelation[] @relation("child")
parents FamilyRelation[] @relation("parent")
}
model FamilyRelation {
child User @relation("child", fields: [childId], references: [id])
childId Int
parent User @relation("parent", fields: [parentId], references: [id])
parentId Int
@@id([parentId, childId])
}
Now I want to query users who has ancestors named “John”.
user
id | name | parent_user_id | child_user_id
1 | "John" | [] | [2, 4]
2 | "A" | [1] | []
3 | "B" | [4] | []
4 | "C" | [1] | [4]
5 | "D" | [] | []
→ query result should be "A", "B", "C".
Toru Eguchi
01/04/2022, 10:24 AMChris Bongers
01/04/2022, 10:36 AMToru Eguchi
01/04/2022, 10:52 AMDo you know the ID of John at this point.No, because I imagine that filtering should be done not only by exact match but also
contain
, startWith
and so on.
Guessing you could use where parent_used_id = 1Even if id is specified before query, is it possible to query “B” ? “B” does not have “John” as a parent. “John” is grand parent from “B”.
Chris Bongers
01/04/2022, 2:29 PMfilter
is a good way to go though