entc/gen: support indexing edge schema with composite primary keys (#2578)

This commit is contained in:
Ariel Mashraki
2022-05-30 17:20:27 +03:00
committed by GitHub
parent 5b81d7d832
commit c26d92d39e
4 changed files with 32 additions and 8 deletions

View File

@@ -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:

View File

@@ -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{

View File

@@ -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"),
}
}

View File

@@ -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"),
}
}