注解

结构注解(Schema annotations) 允许附加元数据到结构对象(例如字段和边) 上面,并且将元数据注入到外部模板中。 注解是一种Go类型,它能进行JSON序列化(例如 struct, map 或 slice),并且需要实现Annotation接口。

内置注解能够配置不同的存储驱动(例如 SQL),控制代码生成输出。

自定义表名

使用 entsql 注解的类型, 可以自定义表名,如下所示:

ent/schema/user.go

  1. package schema
  2. import (
  3. "entgo.io/ent"
  4. "entgo.io/ent/dialect/entsql"
  5. "entgo.io/ent/schema"
  6. "entgo.io/ent/schema/field"
  7. )
  8. // User类型持有用户实体的结构(schema)定义
  9. type User struct {
  10. ent.Schema
  11. }
  12. // 用户实体的注解
  13. func (User) Annotations() []schema.Annotation {
  14. return []schema.Annotation{
  15. entsql.Annotation{Table: "Users"},
  16. }
  17. }
  18. // 用户实体的字段
  19. func (User) Fields() []ent.Field {
  20. return []ent.Field{
  21. field.Int("age"),
  22. field.String("name"),
  23. }
  24. }

Custom Table Schema

Using the Atlas migration engine, an Ent schema can be defined and managed across multiple database schemas. Check out the multi-schema doc for more information.

Foreign Keys Configuration

Ent allows to customize the foreign key creation and provide a referential action for the ON DELETE clause:

ent/schema/user.go

  1. package schema
  2. import (
  3. "entgo.io/ent"
  4. "entgo.io/ent/dialect/entsql"
  5. "entgo.io/ent/schema/edge"
  6. "entgo.io/ent/schema/field"
  7. )
  8. // User类型持有用户实体的结构(schema)定义
  9. type User struct {
  10. ent.Schema
  11. }
  12. // 用户实体的字段
  13. func (User) Fields() []ent.Field {
  14. return []ent.Field{
  15. field.String("name").
  16. Default("Unknown"),
  17. }
  18. }
  19. // 用户实体的关系
  20. func (User) Edges() []ent.Edge {
  21. return []ent.Edge{
  22. edge.To("posts", Post.Type).
  23. Annotations(entsql.OnDelete(entsql.Cascade)),
  24. }
  25. }

The example above configures the foreign key to cascade the deletion of rows in the parent table to the matching rows in the child table.

Database Comments

By default, table and column comments are not stored in the database. However, this functionality can be enabled by using the WithComments(true) annotation. For example:

ent/schema/user.go

  1. package schema
  2. import (
  3. "entgo.io/ent"
  4. "entgo.io/ent/dialect/entsql"
  5. "entgo.io/ent/schema"
  6. "entgo.io/ent/schema/field"
  7. )
  8. // User holds the schema definition for the User entity.
  9. type User struct {
  10. ent.Schema
  11. }
  12. // Annotations of the User.
  13. func (User) Annotations() []schema.Annotation {
  14. return []schema.Annotation{
  15. // Adding this annotation to the schema enables
  16. // comments for the table and all its fields.
  17. entsql.WithComments(true),
  18. schema.Comment("Comment that appears in both the schema and the generated code"),
  19. }
  20. }
  21. // Fields of the User.
  22. func (User) Fields() []ent.Field {
  23. return []ent.Field{
  24. field.String("name").
  25. Comment("The user's name"),
  26. field.Int("age").
  27. Comment("The user's age"),
  28. field.String("skipped").
  29. Comment("This comment won't be stored in the database").
  30. // Explicitly disable comments for this field.
  31. Annotations(
  32. entsql.WithComments(false),
  33. ),
  34. }
  35. }