entc/gen: allow defining index only on edges (#152)

Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/152

Fixes #150

Reviewed By: idoshveki

Differential Revision: D18419810

fbshipit-source-id: aeabd9c3379d4456235884abeca01407569cd7dc
This commit is contained in:
Ariel Mashraki
2019-11-10 02:55:11 -08:00
committed by Facebook Github Bot
parent 782f3d90c9
commit 3ac23d8420
2 changed files with 6 additions and 3 deletions

View File

@@ -377,8 +377,8 @@ func (t Type) Describe(w io.Writer) {
// It fails if the schema index is invalid.
func (t *Type) AddIndex(idx *load.Index) error {
index := &Index{Unique: idx.Unique}
if len(idx.Fields) == 0 {
return fmt.Errorf("missing fields")
if len(idx.Fields) == 0 && len(idx.Edges) == 0 {
return fmt.Errorf("missing fields or edges")
}
for _, name := range idx.Fields {
f, ok := t.fields[name]

View File

@@ -170,7 +170,7 @@ func TestType_AddIndex(t *testing.T) {
)
err = typ.AddIndex(&load.Index{Unique: true})
require.Error(t, err, "missing fields")
require.Error(t, err, "missing fields or edges")
err = typ.AddIndex(&load.Index{Unique: true, Fields: []string{"unknown"}})
require.Error(t, err, "unknown field for index")
@@ -184,6 +184,9 @@ func TestType_AddIndex(t *testing.T) {
err = typ.AddIndex(&load.Index{Unique: true, Fields: []string{"name"}, Edges: []string{"next"}})
require.Error(t, err, "not an inverse edge for O2O relation")
err = typ.AddIndex(&load.Index{Unique: true, Edges: []string{"prev", "owner"}})
require.NoError(t, err, "valid index defined only on edges")
err = typ.AddIndex(&load.Index{Unique: true, Fields: []string{"name"}, Edges: []string{"prev"}})
require.NoError(t, err, "valid index on O2O relation and field")