mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: allow setting default-func for id fields (#1290)
This commit is contained in:
@@ -738,7 +738,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 && f.Info.Type != field.TypeUUID:
|
||||
case f.Unique && f.Default && f.DefaultKind != reflect.Func:
|
||||
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)
|
||||
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
{Name: "before_id", Type: field.TypeFloat64, Nullable: true},
|
||||
{Name: "after_id", Type: field.TypeFloat64, Nullable: true},
|
||||
{Name: "model", Type: field.TypeString},
|
||||
{Name: "pet_cars", Type: field.TypeString, Nullable: true, Size: 25},
|
||||
{Name: "pet_cars", Type: field.TypeString, Nullable: true, Size: 36},
|
||||
}
|
||||
// CarsTable holds the schema information for the "cars" table.
|
||||
CarsTable = &schema.Table{
|
||||
@@ -102,8 +102,8 @@ var (
|
||||
}
|
||||
// PetsColumns holds the columns for the "pets" table.
|
||||
PetsColumns = []*schema.Column{
|
||||
{Name: "id", Type: field.TypeString, Unique: true, Size: 25},
|
||||
{Name: "pet_best_friend", Type: field.TypeString, Unique: true, Nullable: true, Size: 25},
|
||||
{Name: "id", Type: field.TypeString, Unique: true, Size: 36},
|
||||
{Name: "pet_best_friend", Type: field.TypeString, Unique: true, Nullable: true, Size: 36},
|
||||
{Name: "user_pets", Type: field.TypeInt, Nullable: true},
|
||||
}
|
||||
// PetsTable holds the schema information for the "pets" table.
|
||||
@@ -197,8 +197,8 @@ var (
|
||||
}
|
||||
// PetFriendsColumns holds the columns for the "pet_friends" table.
|
||||
PetFriendsColumns = []*schema.Column{
|
||||
{Name: "pet_id", Type: field.TypeString, Size: 25},
|
||||
{Name: "friend_id", Type: field.TypeString, Size: 25},
|
||||
{Name: "pet_id", Type: field.TypeString, Size: 36},
|
||||
{Name: "friend_id", Type: field.TypeString, Size: 36},
|
||||
}
|
||||
// PetFriendsTable holds the schema information for the "pet_friends" table.
|
||||
PetFriendsTable = &schema.Table{
|
||||
|
||||
@@ -80,6 +80,8 @@ func ValidColumn(column string) bool {
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultID holds the default value on creation for the "id" field.
|
||||
DefaultID func() string
|
||||
// IDValidator is a validator for the "id" field. It is called by the builders before save.
|
||||
IDValidator func(string) error
|
||||
)
|
||||
|
||||
@@ -30,6 +30,14 @@ func (pc *PetCreate) SetID(s string) *PetCreate {
|
||||
return pc
|
||||
}
|
||||
|
||||
// SetNillableID sets the "id" field if the given value is not nil.
|
||||
func (pc *PetCreate) SetNillableID(s *string) *PetCreate {
|
||||
if s != nil {
|
||||
pc.SetID(*s)
|
||||
}
|
||||
return pc
|
||||
}
|
||||
|
||||
// SetOwnerID sets the "owner" edge to the User entity by ID.
|
||||
func (pc *PetCreate) SetOwnerID(id int) *PetCreate {
|
||||
pc.mutation.SetOwnerID(id)
|
||||
@@ -109,6 +117,7 @@ func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) {
|
||||
err error
|
||||
node *Pet
|
||||
)
|
||||
pc.defaults()
|
||||
if len(pc.hooks) == 0 {
|
||||
if err = pc.check(); err != nil {
|
||||
return nil, err
|
||||
@@ -147,6 +156,14 @@ func (pc *PetCreate) SaveX(ctx context.Context) *Pet {
|
||||
return v
|
||||
}
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (pc *PetCreate) defaults() {
|
||||
if _, ok := pc.mutation.ID(); !ok {
|
||||
v := pet.DefaultID()
|
||||
pc.mutation.SetID(v)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (pc *PetCreate) check() error {
|
||||
if v, ok := pc.mutation.ID(); ok {
|
||||
@@ -276,6 +293,7 @@ func (pcb *PetCreateBulk) Save(ctx context.Context) ([]*Pet, error) {
|
||||
for i := range pcb.builders {
|
||||
func(i int, root context.Context) {
|
||||
builder := pcb.builders[i]
|
||||
builder.defaults()
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*PetMutation)
|
||||
if !ok {
|
||||
|
||||
@@ -59,6 +59,8 @@ func init() {
|
||||
_ = petFields
|
||||
// petDescID is the schema descriptor for id field.
|
||||
petDescID := petFields[0].Descriptor()
|
||||
// pet.DefaultID holds the default value on creation for the id field.
|
||||
pet.DefaultID = petDescID.Default.(func() string)
|
||||
// pet.IDValidator is a validator for the "id" field. It is called by the builders before save.
|
||||
pet.IDValidator = func() func(string) error {
|
||||
validators := petDescID.Validators
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Pet holds the schema definition for the Pet entity.
|
||||
@@ -19,10 +20,11 @@ type Pet struct {
|
||||
func (Pet) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("id").
|
||||
MaxLen(25).
|
||||
MaxLen(36).
|
||||
NotEmpty().
|
||||
Unique().
|
||||
Immutable(),
|
||||
Immutable().
|
||||
DefaultFunc(uuid.NewString),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user