account_group.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package schema
  2. import (
  3. "time"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect"
  6. "entgo.io/ent/dialect/entsql"
  7. "entgo.io/ent/schema"
  8. "entgo.io/ent/schema/edge"
  9. "entgo.io/ent/schema/field"
  10. "entgo.io/ent/schema/index"
  11. )
  12. // AccountGroup holds the edge schema definition for the account_groups relationship.
  13. // It stores extra fields (priority, created_at) and uses a composite primary key.
  14. type AccountGroup struct {
  15. ent.Schema
  16. }
  17. func (AccountGroup) Annotations() []schema.Annotation {
  18. return []schema.Annotation{
  19. entsql.Annotation{Table: "account_groups"},
  20. // Composite primary key: (account_id, group_id).
  21. field.ID("account_id", "group_id"),
  22. }
  23. }
  24. func (AccountGroup) Fields() []ent.Field {
  25. return []ent.Field{
  26. field.Int64("account_id"),
  27. field.Int64("group_id"),
  28. field.Int("priority").
  29. Default(50),
  30. field.Time("created_at").
  31. Immutable().
  32. Default(time.Now).
  33. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  34. }
  35. }
  36. func (AccountGroup) Edges() []ent.Edge {
  37. return []ent.Edge{
  38. edge.To("account", Account.Type).
  39. Unique().
  40. Required().
  41. Field("account_id"),
  42. edge.To("group", Group.Type).
  43. Unique().
  44. Required().
  45. Field("group_id"),
  46. }
  47. }
  48. func (AccountGroup) Indexes() []ent.Index {
  49. return []ent.Index{
  50. index.Fields("group_id"),
  51. index.Fields("priority"),
  52. }
  53. }