mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/doc: m2m recursive relation example
Reviewed By: alexsn Differential Revision: D17050628 fbshipit-source-id: 32c5cdb25d4ecf5130256468acc4865ce29596bc
This commit is contained in:
committed by
Facebook Github Bot
parent
4c3951d53b
commit
7c3c4ff834
@@ -540,7 +540,7 @@ func (Node) Edges() []ent.Edge {
|
||||
|
||||
- edge.To("children", Node.Type),
|
||||
- edge.From("parent", Node.Type).
|
||||
- Ref("children).
|
||||
- Ref("children").
|
||||
- Unique(),
|
||||
}
|
||||
}
|
||||
@@ -710,6 +710,98 @@ The full example exists in [GitHub](https://github.com/facebookincubator/ent/tre
|
||||
|
||||
## M2M Same Type
|
||||
|
||||

|
||||
|
||||
In this following-followers example, we have a M2M relation between users to its followers. Each user
|
||||
can follow **many** users, and can have **many** followers.
|
||||
|
||||
`ent/schema/user.go`
|
||||
```go
|
||||
// Edges of the User.
|
||||
func (User) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("following", User.Type).
|
||||
From("followers"),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
As you can see, in cases of relations of the same type, you can declare the edge and its
|
||||
reference in the same builder.
|
||||
|
||||
```diff
|
||||
func (User) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
+ edge.To("following", User.Type).
|
||||
+ From("followers"),
|
||||
|
||||
- edge.To("following", User.Type),
|
||||
- edge.From("followers", User.Type).
|
||||
- Ref("following"),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The API for interacting with these edges is as follows:
|
||||
|
||||
```go
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
// Unlike `Save`, `SaveX` panics if an error occurs.
|
||||
a8m := client.User.
|
||||
Create().
|
||||
SetAge(30).
|
||||
SetName("a8m").
|
||||
SaveX(ctx)
|
||||
nati := client.User.
|
||||
Create().
|
||||
SetAge(28).
|
||||
SetName("nati").
|
||||
AddFollowers(a8m).
|
||||
SaveX(ctx)
|
||||
|
||||
// Query following/followers:
|
||||
|
||||
flw := a8m.QueryFollowing().AllX(ctx)
|
||||
fmt.Println(flw)
|
||||
// Output: [User(id=2, age=28, name=nati)]
|
||||
|
||||
flr := a8m.QueryFollowers().AllX(ctx)
|
||||
fmt.Println(flr)
|
||||
// Output: []
|
||||
|
||||
flw = nati.QueryFollowing().AllX(ctx)
|
||||
fmt.Println(flw)
|
||||
// Output: []
|
||||
|
||||
flr = nati.QueryFollowers().AllX(ctx)
|
||||
fmt.Println(flr)
|
||||
// Output: [User(id=1, age=30, name=a8m)]
|
||||
|
||||
// Traverse the graph:
|
||||
|
||||
ages := nati.
|
||||
QueryFollowers(). // [a8m]
|
||||
QueryFollowing(). // [nati]
|
||||
GroupBy(user.FieldAge). // [28]
|
||||
IntsX(ctx)
|
||||
fmt.Println(ages)
|
||||
// Output: [28]
|
||||
|
||||
names := client.User.
|
||||
Query().
|
||||
Where(user.Not(user.HasFollowers())).
|
||||
GroupBy(user.FieldName).
|
||||
StringsX(ctx)
|
||||
fmt.Println(names)
|
||||
// Output: [a8m]
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
The full example exists in [GitHub](https://github.com/facebookincubator/ent/tree/master/examples/m2mrecur).
|
||||
|
||||
|
||||
## M2M Bidirectional
|
||||
|
||||
## Required
|
||||
|
||||
@@ -76,7 +76,7 @@ a {
|
||||
color: #4d8eaa;
|
||||
}
|
||||
|
||||
#er-linked-list, #er-user-spouse, #er-tree {
|
||||
#er-linked-list, #er-user-spouse, #er-tree, #er-following-followers {
|
||||
height: 230px;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user