package main
import (
"context"
"fmt"
"log"
"<entgo.io/ent>"
"<entgo.io/ent/example/privacy"
"<entgo.io/ent/schema/field"
)
// 定义User和Pet的schema。
func ExampleEntClient_Schema() {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// 运行schema的migration创建表。
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
}
// 创建User。
func ExampleEntClient_CreateUser() {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// 创建User。
a8m, err := client.User.
Create().
SetAge(30).
SetName("Ariel").
Save(context.Background())
if err != nil {
log.Fatalf("failed creating user: %v", err)
}
fmt.Println(a8m)
}
// 创建Pet。
func ExampleEntClient_CreatePet() {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// 创建Pet。
coco, err := client.Pet.
Create().
SetName("Coco").
SetOwner(a8m).
Save(context.Background())
if err != nil {
log.Fatalf("failed creating pet: %v", err)
}
fmt.Println(coco)
}
// 查询User。
func ExampleEntClient_QueryUser() {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// 查询User。
a8m, err := client.User.
Query().
Where(user.Name("Ariel")).
Only(context.Background())
if err != nil {
log.Fatalf("failed querying user: %v", err)
}
fmt.Println(a8m)
}
// 查询Pet。
func ExampleEntClient_QueryPet() {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// 查询Pet。
coco, err := client.Pet.
Query().
Whe
评论已关闭