Annotations

Schema annotations allow attaching metadata to schema objects like fields and edges and inject them to external templates. An annotation must be a Go type that is serializable to JSON raw value (e.g. struct, map or slice) and implement the Annotation interface.

The builtin annotations allow configuring the different storage drivers (like SQL) and control the code generation output.

Custom Table Name

A custom table name can be provided for types using the entsql annotation as follows:

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. entsql.Annotation{Table: "Users"},
  16. }
  17. }
  18. // Fields of the User.
  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 holds the schema definition for the User entity.
  9. type User struct {
  10. ent.Schema
  11. }
  12. // Fields of the User.
  13. func (User) Fields() []ent.Field {
  14. return []ent.Field{
  15. field.String("name").
  16. Default("Unknown"),
  17. }
  18. }
  19. // Edges of the User.
  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. }