// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/Wei-Shaw/sub2api/ent/apikey"
	"github.com/Wei-Shaw/sub2api/ent/group"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/ent/usagelog"
	"github.com/Wei-Shaw/sub2api/ent/user"
)

// ApiKeyUpdate is the builder for updating ApiKey entities.
type ApiKeyUpdate struct {
	config
	hooks    []Hook
	mutation *ApiKeyMutation
}

// Where appends a list predicates to the ApiKeyUpdate builder.
func (_u *ApiKeyUpdate) Where(ps ...predicate.ApiKey) *ApiKeyUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *ApiKeyUpdate) SetUpdatedAt(v time.Time) *ApiKeyUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *ApiKeyUpdate) SetDeletedAt(v time.Time) *ApiKeyUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *ApiKeyUpdate) SetNillableDeletedAt(v *time.Time) *ApiKeyUpdate {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *ApiKeyUpdate) ClearDeletedAt() *ApiKeyUpdate {
	_u.mutation.ClearDeletedAt()
	return _u
}

// SetUserID sets the "user_id" field.
func (_u *ApiKeyUpdate) SetUserID(v int64) *ApiKeyUpdate {
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *ApiKeyUpdate) SetNillableUserID(v *int64) *ApiKeyUpdate {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// SetKey sets the "key" field.
func (_u *ApiKeyUpdate) SetKey(v string) *ApiKeyUpdate {
	_u.mutation.SetKey(v)
	return _u
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (_u *ApiKeyUpdate) SetNillableKey(v *string) *ApiKeyUpdate {
	if v != nil {
		_u.SetKey(*v)
	}
	return _u
}

// SetName sets the "name" field.
func (_u *ApiKeyUpdate) SetName(v string) *ApiKeyUpdate {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *ApiKeyUpdate) SetNillableName(v *string) *ApiKeyUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetGroupID sets the "group_id" field.
func (_u *ApiKeyUpdate) SetGroupID(v int64) *ApiKeyUpdate {
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *ApiKeyUpdate) SetNillableGroupID(v *int64) *ApiKeyUpdate {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// ClearGroupID clears the value of the "group_id" field.
func (_u *ApiKeyUpdate) ClearGroupID() *ApiKeyUpdate {
	_u.mutation.ClearGroupID()
	return _u
}

// SetStatus sets the "status" field.
func (_u *ApiKeyUpdate) SetStatus(v string) *ApiKeyUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ApiKeyUpdate) SetNillableStatus(v *string) *ApiKeyUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *ApiKeyUpdate) SetUser(v *User) *ApiKeyUpdate {
	return _u.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *ApiKeyUpdate) SetGroup(v *Group) *ApiKeyUpdate {
	return _u.SetGroupID(v.ID)
}

// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_u *ApiKeyUpdate) AddUsageLogIDs(ids ...int64) *ApiKeyUpdate {
	_u.mutation.AddUsageLogIDs(ids...)
	return _u
}

// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_u *ApiKeyUpdate) AddUsageLogs(v ...*UsageLog) *ApiKeyUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUsageLogIDs(ids...)
}

// Mutation returns the ApiKeyMutation object of the builder.
func (_u *ApiKeyUpdate) Mutation() *ApiKeyMutation {
	return _u.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (_u *ApiKeyUpdate) ClearUser() *ApiKeyUpdate {
	_u.mutation.ClearUser()
	return _u
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *ApiKeyUpdate) ClearGroup() *ApiKeyUpdate {
	_u.mutation.ClearGroup()
	return _u
}

// ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
func (_u *ApiKeyUpdate) ClearUsageLogs() *ApiKeyUpdate {
	_u.mutation.ClearUsageLogs()
	return _u
}

// RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
func (_u *ApiKeyUpdate) RemoveUsageLogIDs(ids ...int64) *ApiKeyUpdate {
	_u.mutation.RemoveUsageLogIDs(ids...)
	return _u
}

// RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
func (_u *ApiKeyUpdate) RemoveUsageLogs(v ...*UsageLog) *ApiKeyUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUsageLogIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *ApiKeyUpdate) Save(ctx context.Context) (int, error) {
	if err := _u.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *ApiKeyUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *ApiKeyUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *ApiKeyUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *ApiKeyUpdate) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if apikey.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *ApiKeyUpdate) check() error {
	if v, ok := _u.mutation.Key(); ok {
		if err := apikey.KeyValidator(v); err != nil {
			return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "ApiKey.key": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Name(); ok {
		if err := apikey.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "ApiKey.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := apikey.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "ApiKey.status": %w`, err)}
		}
	}
	if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "ApiKey.user"`)
	}
	return nil
}

func (_u *ApiKeyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(apikey.Table, apikey.Columns, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(apikey.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Key(); ok {
		_spec.SetField(apikey.FieldKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(apikey.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(apikey.FieldStatus, field.TypeString, value)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.UserTable,
			Columns: []string{apikey.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.UserTable,
			Columns: []string{apikey.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.GroupTable,
			Columns: []string{apikey.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.GroupTable,
			Columns: []string{apikey.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apikey.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ApiKeyUpdateOne is the builder for updating a single ApiKey entity.
type ApiKeyUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ApiKeyMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *ApiKeyUpdateOne) SetUpdatedAt(v time.Time) *ApiKeyUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *ApiKeyUpdateOne) SetDeletedAt(v time.Time) *ApiKeyUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *ApiKeyUpdateOne) SetNillableDeletedAt(v *time.Time) *ApiKeyUpdateOne {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *ApiKeyUpdateOne) ClearDeletedAt() *ApiKeyUpdateOne {
	_u.mutation.ClearDeletedAt()
	return _u
}

// SetUserID sets the "user_id" field.
func (_u *ApiKeyUpdateOne) SetUserID(v int64) *ApiKeyUpdateOne {
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *ApiKeyUpdateOne) SetNillableUserID(v *int64) *ApiKeyUpdateOne {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// SetKey sets the "key" field.
func (_u *ApiKeyUpdateOne) SetKey(v string) *ApiKeyUpdateOne {
	_u.mutation.SetKey(v)
	return _u
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (_u *ApiKeyUpdateOne) SetNillableKey(v *string) *ApiKeyUpdateOne {
	if v != nil {
		_u.SetKey(*v)
	}
	return _u
}

// SetName sets the "name" field.
func (_u *ApiKeyUpdateOne) SetName(v string) *ApiKeyUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *ApiKeyUpdateOne) SetNillableName(v *string) *ApiKeyUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetGroupID sets the "group_id" field.
func (_u *ApiKeyUpdateOne) SetGroupID(v int64) *ApiKeyUpdateOne {
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *ApiKeyUpdateOne) SetNillableGroupID(v *int64) *ApiKeyUpdateOne {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// ClearGroupID clears the value of the "group_id" field.
func (_u *ApiKeyUpdateOne) ClearGroupID() *ApiKeyUpdateOne {
	_u.mutation.ClearGroupID()
	return _u
}

// SetStatus sets the "status" field.
func (_u *ApiKeyUpdateOne) SetStatus(v string) *ApiKeyUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ApiKeyUpdateOne) SetNillableStatus(v *string) *ApiKeyUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *ApiKeyUpdateOne) SetUser(v *User) *ApiKeyUpdateOne {
	return _u.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *ApiKeyUpdateOne) SetGroup(v *Group) *ApiKeyUpdateOne {
	return _u.SetGroupID(v.ID)
}

// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_u *ApiKeyUpdateOne) AddUsageLogIDs(ids ...int64) *ApiKeyUpdateOne {
	_u.mutation.AddUsageLogIDs(ids...)
	return _u
}

// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_u *ApiKeyUpdateOne) AddUsageLogs(v ...*UsageLog) *ApiKeyUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddUsageLogIDs(ids...)
}

// Mutation returns the ApiKeyMutation object of the builder.
func (_u *ApiKeyUpdateOne) Mutation() *ApiKeyMutation {
	return _u.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (_u *ApiKeyUpdateOne) ClearUser() *ApiKeyUpdateOne {
	_u.mutation.ClearUser()
	return _u
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *ApiKeyUpdateOne) ClearGroup() *ApiKeyUpdateOne {
	_u.mutation.ClearGroup()
	return _u
}

// ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
func (_u *ApiKeyUpdateOne) ClearUsageLogs() *ApiKeyUpdateOne {
	_u.mutation.ClearUsageLogs()
	return _u
}

// RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
func (_u *ApiKeyUpdateOne) RemoveUsageLogIDs(ids ...int64) *ApiKeyUpdateOne {
	_u.mutation.RemoveUsageLogIDs(ids...)
	return _u
}

// RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
func (_u *ApiKeyUpdateOne) RemoveUsageLogs(v ...*UsageLog) *ApiKeyUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveUsageLogIDs(ids...)
}

// Where appends a list predicates to the ApiKeyUpdate builder.
func (_u *ApiKeyUpdateOne) Where(ps ...predicate.ApiKey) *ApiKeyUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *ApiKeyUpdateOne) Select(field string, fields ...string) *ApiKeyUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated ApiKey entity.
func (_u *ApiKeyUpdateOne) Save(ctx context.Context) (*ApiKey, error) {
	if err := _u.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *ApiKeyUpdateOne) SaveX(ctx context.Context) *ApiKey {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *ApiKeyUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *ApiKeyUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *ApiKeyUpdateOne) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if apikey.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *ApiKeyUpdateOne) check() error {
	if v, ok := _u.mutation.Key(); ok {
		if err := apikey.KeyValidator(v); err != nil {
			return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "ApiKey.key": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Name(); ok {
		if err := apikey.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "ApiKey.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := apikey.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "ApiKey.status": %w`, err)}
		}
	}
	if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "ApiKey.user"`)
	}
	return nil
}

func (_u *ApiKeyUpdateOne) sqlSave(ctx context.Context) (_node *ApiKey, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(apikey.Table, apikey.Columns, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ApiKey.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, apikey.FieldID)
		for _, f := range fields {
			if !apikey.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != apikey.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(apikey.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Key(); ok {
		_spec.SetField(apikey.FieldKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(apikey.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(apikey.FieldStatus, field.TypeString, value)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.UserTable,
			Columns: []string{apikey.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.UserTable,
			Columns: []string{apikey.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.GroupTable,
			Columns: []string{apikey.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.GroupTable,
			Columns: []string{apikey.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &ApiKey{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apikey.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
