entc/gen: allow setting unique fields with default values of type function (#610)

Fixed #609
This commit is contained in:
Ariel Mashraki
2020-07-16 18:57:08 +03:00
committed by GitHub
parent 9d42b5ef40
commit 4e9fd67a3e
3 changed files with 4 additions and 3 deletions

View File

@@ -575,7 +575,7 @@ func (t *Type) checkField(tf *Field, f *load.Field) (err error) {
err = fmt.Errorf("invalid type for field %s", f.Name)
case f.Nillable && !f.Optional:
err = fmt.Errorf("nillable field %q must be optional", f.Name)
case f.Unique && f.Default:
case f.Unique && f.Default && f.Info.Type != field.TypeUUID:
err = fmt.Errorf("unique field %q cannot have default value", f.Name)
case t.fields[f.Name] != nil:
err = fmt.Errorf("field %q redeclared for type %q", f.Name, t.Name)

View File

@@ -15,7 +15,7 @@ var (
// BlobsColumns holds the columns for the "blobs" table.
BlobsColumns = []*schema.Column{
{Name: "id", Type: field.TypeUUID},
{Name: "uuid", Type: field.TypeUUID},
{Name: "uuid", Type: field.TypeUUID, Unique: true},
{Name: "blob_parent", Type: field.TypeUUID, Unique: true, Nullable: true},
}
// BlobsTable holds the schema information for the "blobs" table.

View File

@@ -23,7 +23,8 @@ func (Blob) Fields() []ent.Field {
field.UUID("id", uuid.UUID{}).
Default(uuid.New),
field.UUID("uuid", uuid.UUID{}).
Default(uuid.New),
Default(uuid.New).
Unique(),
}
}