user_allowed_group.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // UserAllowedGroup holds the edge schema definition for the user_allowed_groups relationship.
  13. // It replaces the legacy users.allowed_groups BIGINT[] column.
  14. type UserAllowedGroup struct {
  15. ent.Schema
  16. }
  17. func (UserAllowedGroup) Annotations() []schema.Annotation {
  18. return []schema.Annotation{
  19. entsql.Annotation{Table: "user_allowed_groups"},
  20. // Composite primary key: (user_id, group_id).
  21. field.ID("user_id", "group_id"),
  22. }
  23. }
  24. func (UserAllowedGroup) Fields() []ent.Field {
  25. return []ent.Field{
  26. field.Int64("user_id"),
  27. field.Int64("group_id"),
  28. field.Time("created_at").
  29. Immutable().
  30. Default(time.Now).
  31. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  32. }
  33. }
  34. func (UserAllowedGroup) Edges() []ent.Edge {
  35. return []ent.Edge{
  36. edge.To("user", User.Type).
  37. Unique().
  38. Required().
  39. Field("user_id"),
  40. edge.To("group", Group.Type).
  41. Unique().
  42. Required().
  43. Field("group_id"),
  44. }
  45. }
  46. func (UserAllowedGroup) Indexes() []ent.Index {
  47. return []ent.Index{
  48. index.Fields("group_id"),
  49. }
  50. }