Introduction
Quick Summary
Schema describes the definition of one entity type in the graph, like User
or Group
, and can contain the following configurations:
- Entity fields (or properties), like: name or age of a
User
. - Entity edges (or relations), like:
User
's groups, orUser
's friends. - Database specific options, like: indexes or unique indexes.
Here's an example of a schema:
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/index"
)
type User struct {
ent.Schema
}
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("age"),
field.String("name"),
field.String("nickname").
Unique(),
}
}
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("groups", Group.Type),
edge.To("friends", User.Type),
}
}
func (User) Indexes() []ent.Index {
return []ent.Index{
index.Fields("age", "name").
Unique(),
}
}
Entity schemas are usually stored inside ent/schema
directory under the root directory of your project, and can be generated by entc
as follows:
go run -mod=mod entgo.io/ent/cmd/ent new User Group
Client
) are not available due to internal use. You can circumvent reserved names by using an annotation as mentioned here. :::