索引
复合索引
为了提高数据检索的速度或确保数据的唯一性,可以在一个或多个字段上配置索引。
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(),
}
}注意,要将单个字段设为唯一索引,请像下面的例子一样在构建器中使用 Unique 方法。
边的索引
Indexes can be configured on composition of fields and edges. The main use-case is setting uniqueness on fields under a specific relation. Let's take an example: 可以配置字段和边组成的索引,这主要用于确保某些关系下数据的唯一性。看一个例子:

In the example above, we have a City with many Streets, and we want to set the street name to be unique under each city. 上面的例子中,一个 City 有多条 Street, 我们想确保街道名在其所属的城市内是唯一的。
ent/schema/city.go
ent/schema/street.go
example.go
完整的例子请查看 GitHub.
支持的方言
目前不支持 Gremlin 的索引,只支持 SQL 的索引。
Last updated
Was this helpful?
