diff --git a/entc/gen/type.go b/entc/gen/type.go index 56bac39c5..881dd2b58 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -125,11 +125,13 @@ func NewType(c Config, schema *load.Schema) (*Type, error) { fields: make(map[string]*Field, len(schema.Fields)), } for i, f := range schema.Fields { - if !f.Type.Valid() { + switch { + case !f.Type.Valid(): return nil, fmt.Errorf("invalid type for field %s", f.Name) - } - if f.Nillable && !f.Optional { + case f.Nillable && !f.Optional: return nil, fmt.Errorf("nillable field %q must be optional", f.Name) + case f.Unique && f.Default: + return nil, fmt.Errorf("unique field %q can not have default value", f.Name) } typ.Fields[i] = &Field{ def: f, diff --git a/entc/gen/type_test.go b/entc/gen/type_test.go index 0a2c2018c..b2d5253f5 100644 --- a/entc/gen/type_test.go +++ b/entc/gen/type_test.go @@ -20,6 +20,14 @@ func TestType(t *testing.T) { require.Equal("t1", typ.Label()) require.Equal("t1", typ.Package()) require.Equal("t", typ.Receiver()) + + typ, err = NewType(Config{Package: "entc/gen"}, &load.Schema{ + Fields: []*load.Field{ + {Unique: true, Default: true, Type: field.TypeInt}, + }, + }) + require.Error(err, "unique field can not have default") + require.Nil(typ) } func TestType_Label(t *testing.T) {