mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
entc/integ: add generated-id upsert example
This commit is contained in:
committed by
Ariel Mashraki
parent
d1593150ee
commit
7e3785f105
@@ -31,6 +31,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
|
||||
)
|
||||
|
||||
@@ -31,6 +31,14 @@ func (ic *ItemCreate) SetID(s string) *ItemCreate {
|
||||
return ic
|
||||
}
|
||||
|
||||
// SetNillableID sets the "id" field if the given value is not nil.
|
||||
func (ic *ItemCreate) SetNillableID(s *string) *ItemCreate {
|
||||
if s != nil {
|
||||
ic.SetID(*s)
|
||||
}
|
||||
return ic
|
||||
}
|
||||
|
||||
// Mutation returns the ItemMutation object of the builder.
|
||||
func (ic *ItemCreate) Mutation() *ItemMutation {
|
||||
return ic.mutation
|
||||
@@ -42,6 +50,7 @@ func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) {
|
||||
err error
|
||||
node *Item
|
||||
)
|
||||
ic.defaults()
|
||||
if len(ic.hooks) == 0 {
|
||||
if err = ic.check(); err != nil {
|
||||
return nil, err
|
||||
@@ -99,6 +108,14 @@ func (ic *ItemCreate) ExecX(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (ic *ItemCreate) defaults() {
|
||||
if _, ok := ic.mutation.ID(); !ok {
|
||||
v := item.DefaultID()
|
||||
ic.mutation.SetID(v)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (ic *ItemCreate) check() error {
|
||||
if v, ok := ic.mutation.ID(); ok {
|
||||
@@ -272,6 +289,7 @@ func (icb *ItemCreateBulk) Save(ctx context.Context) ([]*Item, error) {
|
||||
for i := range icb.builders {
|
||||
func(i int, root context.Context) {
|
||||
builder := icb.builders[i]
|
||||
builder.defaults()
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*ItemMutation)
|
||||
if !ok {
|
||||
|
||||
@@ -179,6 +179,8 @@ func init() {
|
||||
_ = itemFields
|
||||
// itemDescID is the schema descriptor for id field.
|
||||
itemDescID := itemFields[0].Descriptor()
|
||||
// item.DefaultID holds the default value on creation for the id field.
|
||||
item.DefaultID = itemDescID.Default.(func() string)
|
||||
// item.IDValidator is a validator for the "id" field. It is called by the builders before save.
|
||||
item.IDValidator = itemDescID.Validators[0].(func(string) error)
|
||||
petFields := schema.Pet{}.Fields()
|
||||
|
||||
@@ -7,6 +7,8 @@ package schema
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Item holds the schema definition for the Item entity.
|
||||
@@ -18,6 +20,7 @@ type Item struct {
|
||||
func (Item) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("id").
|
||||
DefaultFunc(uuid.NewString).
|
||||
MaxLen(64),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ const (
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
@@ -29,6 +29,14 @@ func (ic *ItemCreate) SetID(s string) *ItemCreate {
|
||||
return ic
|
||||
}
|
||||
|
||||
// SetNillableID sets the "id" field if the given value is not nil.
|
||||
func (ic *ItemCreate) SetNillableID(s *string) *ItemCreate {
|
||||
if s != nil {
|
||||
ic.SetID(*s)
|
||||
}
|
||||
return ic
|
||||
}
|
||||
|
||||
// Mutation returns the ItemMutation object of the builder.
|
||||
func (ic *ItemCreate) Mutation() *ItemMutation {
|
||||
return ic.mutation
|
||||
@@ -40,6 +48,7 @@ func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) {
|
||||
err error
|
||||
node *Item
|
||||
)
|
||||
ic.defaults()
|
||||
if len(ic.hooks) == 0 {
|
||||
if err = ic.check(); err != nil {
|
||||
return nil, err
|
||||
@@ -97,6 +106,14 @@ func (ic *ItemCreate) ExecX(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (ic *ItemCreate) defaults() {
|
||||
if _, ok := ic.mutation.ID(); !ok {
|
||||
v := item.DefaultID()
|
||||
ic.mutation.SetID(v)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (ic *ItemCreate) check() error {
|
||||
if v, ok := ic.mutation.ID(); ok {
|
||||
|
||||
@@ -179,6 +179,8 @@ func init() {
|
||||
_ = itemFields
|
||||
// itemDescID is the schema descriptor for id field.
|
||||
itemDescID := itemFields[0].Descriptor()
|
||||
// item.DefaultID holds the default value on creation for the id field.
|
||||
item.DefaultID = itemDescID.Default.(func() string)
|
||||
// item.IDValidator is a validator for the "id" field. It is called by the builders before save.
|
||||
item.IDValidator = itemDescID.Validators[0].(func(string) error)
|
||||
petFields := schema.Pet{}.Fields()
|
||||
|
||||
@@ -308,8 +308,16 @@ func Upsert(t *testing.T, client *ent.Client) {
|
||||
require.Equal(t, "B", users[1].Name)
|
||||
|
||||
// Setting primary key.
|
||||
client.Item.Create().SetID("A").SaveX(ctx)
|
||||
client.Item.Create().SetID("A").OnConflict(sql.ConflictColumns(item.FieldID)).Ignore().ExecX(ctx)
|
||||
a := client.Item.Create().SetID("A").SaveX(ctx)
|
||||
require.Equal(t, "A", a.ID)
|
||||
aid := client.Item.Create().SetID("A").OnConflict(sql.ConflictColumns(item.FieldID)).Ignore().IDX(ctx)
|
||||
require.Equal(t, a.ID, aid)
|
||||
|
||||
// Primary key is generated.
|
||||
b := client.Item.Create().SaveX(ctx)
|
||||
require.NotZero(t, b.ID)
|
||||
bid := client.Item.Create().SetID(b.ID).OnConflictColumns(item.FieldID).Ignore().IDX(ctx)
|
||||
require.Equal(t, b.ID, bid)
|
||||
}
|
||||
|
||||
func Clone(t *testing.T, client *ent.Client) {
|
||||
|
||||
Reference in New Issue
Block a user