diff --git a/entc/gen/type.go b/entc/gen/type.go index a0733f721..4c57c770f 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -619,14 +619,10 @@ func (t *Type) AddIndex(idx *load.Index) error { } for _, name := range idx.Fields { var f *Field - if name == t.ID.Name { + if t.HasOneFieldID() && name == t.ID.Name { f = t.ID - } else { - var ok bool - f, ok = t.fields[name] - if !ok { - return fmt.Errorf("unknown index field %q", name) - } + } else if f = t.fields[name]; f == nil { + return fmt.Errorf("unknown index field %q", name) } index.Columns = append(index.Columns, f.StorageKey()) } @@ -640,7 +636,7 @@ func (t *Type) AddIndex(idx *load.Index) error { } switch { case ed == nil: - return fmt.Errorf("unknown index field %q", name) + return fmt.Errorf("unknown index edge %q", name) case ed.Rel.Type == O2O && !ed.IsInverse(): return fmt.Errorf("non-inverse edge (edge.From) for index %q on O2O relation", name) case ed.Rel.Type != M2O && ed.Rel.Type != O2O: diff --git a/entc/integration/edgeschema/ent/migrate/schema.go b/entc/integration/edgeschema/ent/migrate/schema.go index a84191d86..f707f3f83 100644 --- a/entc/integration/edgeschema/ent/migrate/schema.go +++ b/entc/integration/edgeschema/ent/migrate/schema.go @@ -41,6 +41,11 @@ var ( Unique: true, Columns: []*schema.Column{FriendshipsColumns[3], FriendshipsColumns[4]}, }, + { + Name: "friendship_created_at", + Unique: false, + Columns: []*schema.Column{FriendshipsColumns[2]}, + }, }, } // GroupsColumns holds the columns for the "groups" table. @@ -79,6 +84,13 @@ var ( OnDelete: schema.NoAction, }, }, + Indexes: []*schema.Index{ + { + Name: "relationship_weight", + Unique: false, + Columns: []*schema.Column{RelationshipsColumns[0]}, + }, + }, } // TweetsColumns holds the columns for the "tweets" table. TweetsColumns = []*schema.Column{ diff --git a/entc/integration/edgeschema/ent/schema/friendship.go b/entc/integration/edgeschema/ent/schema/friendship.go index 780af70d7..720ea0717 100644 --- a/entc/integration/edgeschema/ent/schema/friendship.go +++ b/entc/integration/edgeschema/ent/schema/friendship.go @@ -10,6 +10,7 @@ import ( "entgo.io/ent" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" ) // Friendship holds the edge schema definition of the Friendship relationship. @@ -42,3 +43,10 @@ func (Friendship) Edges() []ent.Edge { Field("friend_id"), } } + +// Indexes of the Friendship. +func (Friendship) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("created_at"), + } +} diff --git a/entc/integration/edgeschema/ent/schema/relationship.go b/entc/integration/edgeschema/ent/schema/relationship.go index 3d4489571..a88fc9755 100644 --- a/entc/integration/edgeschema/ent/schema/relationship.go +++ b/entc/integration/edgeschema/ent/schema/relationship.go @@ -9,6 +9,7 @@ import ( "entgo.io/ent/schema" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" ) // Relationship holds the schema definition for the Relationship entity. @@ -45,3 +46,10 @@ func (Relationship) Edges() []ent.Edge { Field("relative_id"), } } + +// Indexes of the Relationship. +func (Relationship) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("weight"), + } +}