ent 基本使用八 索引

我们可以在ent 的schema 中定义index 可以方便的控制数据约束,使用索引可以加速我们的访问以及数据的唯一性处理

配置字段索引

  • 多字段索引
package schema
import (
    "github.com/facebookincubator/ent"
    "github.com/facebookincubator/ent/schema/index"
)
// User holds the schema definition for the User entity.
type User struct {
    ent.Schema
}
func (User) Indexes() []ent.Index {
    return []ent.Index{
        // non-unique index.
        index.Fields("field1", "field2"),
        // unique index.
        index.Fields("first_name", "last_name").
            Unique(),
    }
}
 
 
  • 配置单独字段索引
func (User) Fields() []ent.Field {
    return []ent.Field{
        field.String("phone").
            Unique(),
    }
}

边上的索引

可以根据字段和边的组成来配置索引。主要用例是在特定关系下的字段上设置唯一性,一个例子

 

 


在上面的示例中,我们有一个City带有许多Street的,我们希望将街道名称设置为每个城市下唯一。
city schema

// City holds the schema definition for the City entity.
type City struct {
    ent.Schema
}
// Fields of the City.
func (City) Fields() []ent.Field {
    return []ent.Field{
        field.String("name"),
    }
}
// Edges of the City.
func (City) Edges() []ent.Edge {
    return []ent.Edge{
        edge.To("streets", Street.Type),
    }
}
 
 

street schema

// Street holds the schema definition for the Street entity.
type Street struct {
    ent.Schema
}
// Fields of the Street.
func (Street) Fields() []ent.Field {
    return []ent.Field{
        field.String("name"),
    }
}
// Edges of the Street.
func (Street) Edges() []ent.Edge {
    return []ent.Edge{
        edge.From("city", City.Type).
            Ref("streets").
            Unique(),
    }
}
// Indexes of the Street.
func (Street) Indexes() []ent.Index {
    return []ent.Index{
        index.Fields("name").
            Edges("city").
            Unique(),
    }
}
 
  • 测试代码
package main
import (
    "context"
    "fmt"
    "log"
    _ "github.com/go-sql-driver/mysql"
    "github.com/rongfengliang/ent-index/ent"
)
func main() {
    client, err := ent.Open("mysql", "root:dalongrong@tcp(127.0.0.1)/gogs")
    if err != nil {
        log.Fatalf("failed opening connection to sqlite: %v", err)
    }
    defer client.Close()
    ctx := context.Background()
    Do(ctx, client)
}
func Do(ctx context.Context, client *ent.Client) error {
    // Unlike `Save`, `SaveX` panics if an error occurs.
    tlv := client.City.
        Create().
        SetName("TLV").
        SaveX(ctx)
    nyc := client.City.
        Create().
        SetName("NYC").
        SaveX(ctx)
    // Add a street "ST" to "TLV".
    client.Street.
        Create().
        SetName("ST").
        SetCity(tlv).
        SaveX(ctx)
    // This operation will fail because "ST"
    // is already created under "TLV".
    _, err := client.Street.
        Create().
        SetName("ST").
        SetCity(tlv).
        Save(ctx)
    if err == nil {
        return fmt.Errorf("expecting creation to fail")
    }
    // Add a street "ST" to "NYC".
    client.Street.
        Create().
        SetName("ST").
        SetCity(nyc).
        SaveX(ctx)
    return nil
}

说明

索引当前仅支持SQL方言,不支持Gremlin。

参考资料

https://entgo.io/docs/schema-indexes/

posted on 2019-10-14 23:03  荣锋亮  阅读(510)  评论(0编辑  收藏  举报

导航