// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"demo/ent/course"
	"demo/ent/department"
	"demo/ent/instructor"
	"demo/ent/predicate"
	"demo/ent/student"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// DepartmentUpdate is the builder for updating Department entities.
type DepartmentUpdate struct {
	config
	hooks    []Hook
	mutation *DepartmentMutation
}

// Where appends a list predicates to the DepartmentUpdate builder.
func (du *DepartmentUpdate) Where(ps ...predicate.Department) *DepartmentUpdate {
	du.mutation.Where(ps...)
	return du
}

// SetName sets the "name" field.
func (du *DepartmentUpdate) SetName(s string) *DepartmentUpdate {
	du.mutation.SetName(s)
	return du
}

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

// AddInstructorIDs adds the "instructors" edge to the Instructor entity by IDs.
func (du *DepartmentUpdate) AddInstructorIDs(ids ...int) *DepartmentUpdate {
	du.mutation.AddInstructorIDs(ids...)
	return du
}

// AddInstructors adds the "instructors" edges to the Instructor entity.
func (du *DepartmentUpdate) AddInstructors(i ...*Instructor) *DepartmentUpdate {
	ids := make([]int, len(i))
	for j := range i {
		ids[j] = i[j].ID
	}
	return du.AddInstructorIDs(ids...)
}

// AddCourseIDs adds the "courses" edge to the Course entity by IDs.
func (du *DepartmentUpdate) AddCourseIDs(ids ...int) *DepartmentUpdate {
	du.mutation.AddCourseIDs(ids...)
	return du
}

// AddCourses adds the "courses" edges to the Course entity.
func (du *DepartmentUpdate) AddCourses(c ...*Course) *DepartmentUpdate {
	ids := make([]int, len(c))
	for i := range c {
		ids[i] = c[i].ID
	}
	return du.AddCourseIDs(ids...)
}

// AddStudentIDs adds the "students" edge to the Student entity by IDs.
func (du *DepartmentUpdate) AddStudentIDs(ids ...int) *DepartmentUpdate {
	du.mutation.AddStudentIDs(ids...)
	return du
}

// AddStudents adds the "students" edges to the Student entity.
func (du *DepartmentUpdate) AddStudents(s ...*Student) *DepartmentUpdate {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return du.AddStudentIDs(ids...)
}

// Mutation returns the DepartmentMutation object of the builder.
func (du *DepartmentUpdate) Mutation() *DepartmentMutation {
	return du.mutation
}

// ClearInstructors clears all "instructors" edges to the Instructor entity.
func (du *DepartmentUpdate) ClearInstructors() *DepartmentUpdate {
	du.mutation.ClearInstructors()
	return du
}

// RemoveInstructorIDs removes the "instructors" edge to Instructor entities by IDs.
func (du *DepartmentUpdate) RemoveInstructorIDs(ids ...int) *DepartmentUpdate {
	du.mutation.RemoveInstructorIDs(ids...)
	return du
}

// RemoveInstructors removes "instructors" edges to Instructor entities.
func (du *DepartmentUpdate) RemoveInstructors(i ...*Instructor) *DepartmentUpdate {
	ids := make([]int, len(i))
	for j := range i {
		ids[j] = i[j].ID
	}
	return du.RemoveInstructorIDs(ids...)
}

// ClearCourses clears all "courses" edges to the Course entity.
func (du *DepartmentUpdate) ClearCourses() *DepartmentUpdate {
	du.mutation.ClearCourses()
	return du
}

// RemoveCourseIDs removes the "courses" edge to Course entities by IDs.
func (du *DepartmentUpdate) RemoveCourseIDs(ids ...int) *DepartmentUpdate {
	du.mutation.RemoveCourseIDs(ids...)
	return du
}

// RemoveCourses removes "courses" edges to Course entities.
func (du *DepartmentUpdate) RemoveCourses(c ...*Course) *DepartmentUpdate {
	ids := make([]int, len(c))
	for i := range c {
		ids[i] = c[i].ID
	}
	return du.RemoveCourseIDs(ids...)
}

// ClearStudents clears all "students" edges to the Student entity.
func (du *DepartmentUpdate) ClearStudents() *DepartmentUpdate {
	du.mutation.ClearStudents()
	return du
}

// RemoveStudentIDs removes the "students" edge to Student entities by IDs.
func (du *DepartmentUpdate) RemoveStudentIDs(ids ...int) *DepartmentUpdate {
	du.mutation.RemoveStudentIDs(ids...)
	return du
}

// RemoveStudents removes "students" edges to Student entities.
func (du *DepartmentUpdate) RemoveStudents(s ...*Student) *DepartmentUpdate {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return du.RemoveStudentIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (du *DepartmentUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, du.sqlSave, du.mutation, du.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (du *DepartmentUpdate) check() error {
	if v, ok := du.mutation.Name(); ok {
		if err := department.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Department.name": %w`, err)}
		}
	}
	return nil
}

func (du *DepartmentUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := du.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(department.Table, department.Columns, sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt))
	if ps := du.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := du.mutation.Name(); ok {
		_spec.SetField(department.FieldName, field.TypeString, value)
	}
	if du.mutation.InstructorsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.InstructorsTable,
			Columns: []string{department.InstructorsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.RemovedInstructorsIDs(); len(nodes) > 0 && !du.mutation.InstructorsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.InstructorsTable,
			Columns: []string{department.InstructorsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.InstructorsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.InstructorsTable,
			Columns: []string{department.InstructorsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if du.mutation.CoursesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.CoursesTable,
			Columns: []string{department.CoursesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.RemovedCoursesIDs(); len(nodes) > 0 && !du.mutation.CoursesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.CoursesTable,
			Columns: []string{department.CoursesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.CoursesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.CoursesTable,
			Columns: []string{department.CoursesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if du.mutation.StudentsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.StudentsTable,
			Columns: []string{department.StudentsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.RemovedStudentsIDs(); len(nodes) > 0 && !du.mutation.StudentsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.StudentsTable,
			Columns: []string{department.StudentsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := du.mutation.StudentsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.StudentsTable,
			Columns: []string{department.StudentsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, du.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{department.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	du.mutation.done = true
	return n, nil
}

// DepartmentUpdateOne is the builder for updating a single Department entity.
type DepartmentUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DepartmentMutation
}

// SetName sets the "name" field.
func (duo *DepartmentUpdateOne) SetName(s string) *DepartmentUpdateOne {
	duo.mutation.SetName(s)
	return duo
}

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

// AddInstructorIDs adds the "instructors" edge to the Instructor entity by IDs.
func (duo *DepartmentUpdateOne) AddInstructorIDs(ids ...int) *DepartmentUpdateOne {
	duo.mutation.AddInstructorIDs(ids...)
	return duo
}

// AddInstructors adds the "instructors" edges to the Instructor entity.
func (duo *DepartmentUpdateOne) AddInstructors(i ...*Instructor) *DepartmentUpdateOne {
	ids := make([]int, len(i))
	for j := range i {
		ids[j] = i[j].ID
	}
	return duo.AddInstructorIDs(ids...)
}

// AddCourseIDs adds the "courses" edge to the Course entity by IDs.
func (duo *DepartmentUpdateOne) AddCourseIDs(ids ...int) *DepartmentUpdateOne {
	duo.mutation.AddCourseIDs(ids...)
	return duo
}

// AddCourses adds the "courses" edges to the Course entity.
func (duo *DepartmentUpdateOne) AddCourses(c ...*Course) *DepartmentUpdateOne {
	ids := make([]int, len(c))
	for i := range c {
		ids[i] = c[i].ID
	}
	return duo.AddCourseIDs(ids...)
}

// AddStudentIDs adds the "students" edge to the Student entity by IDs.
func (duo *DepartmentUpdateOne) AddStudentIDs(ids ...int) *DepartmentUpdateOne {
	duo.mutation.AddStudentIDs(ids...)
	return duo
}

// AddStudents adds the "students" edges to the Student entity.
func (duo *DepartmentUpdateOne) AddStudents(s ...*Student) *DepartmentUpdateOne {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return duo.AddStudentIDs(ids...)
}

// Mutation returns the DepartmentMutation object of the builder.
func (duo *DepartmentUpdateOne) Mutation() *DepartmentMutation {
	return duo.mutation
}

// ClearInstructors clears all "instructors" edges to the Instructor entity.
func (duo *DepartmentUpdateOne) ClearInstructors() *DepartmentUpdateOne {
	duo.mutation.ClearInstructors()
	return duo
}

// RemoveInstructorIDs removes the "instructors" edge to Instructor entities by IDs.
func (duo *DepartmentUpdateOne) RemoveInstructorIDs(ids ...int) *DepartmentUpdateOne {
	duo.mutation.RemoveInstructorIDs(ids...)
	return duo
}

// RemoveInstructors removes "instructors" edges to Instructor entities.
func (duo *DepartmentUpdateOne) RemoveInstructors(i ...*Instructor) *DepartmentUpdateOne {
	ids := make([]int, len(i))
	for j := range i {
		ids[j] = i[j].ID
	}
	return duo.RemoveInstructorIDs(ids...)
}

// ClearCourses clears all "courses" edges to the Course entity.
func (duo *DepartmentUpdateOne) ClearCourses() *DepartmentUpdateOne {
	duo.mutation.ClearCourses()
	return duo
}

// RemoveCourseIDs removes the "courses" edge to Course entities by IDs.
func (duo *DepartmentUpdateOne) RemoveCourseIDs(ids ...int) *DepartmentUpdateOne {
	duo.mutation.RemoveCourseIDs(ids...)
	return duo
}

// RemoveCourses removes "courses" edges to Course entities.
func (duo *DepartmentUpdateOne) RemoveCourses(c ...*Course) *DepartmentUpdateOne {
	ids := make([]int, len(c))
	for i := range c {
		ids[i] = c[i].ID
	}
	return duo.RemoveCourseIDs(ids...)
}

// ClearStudents clears all "students" edges to the Student entity.
func (duo *DepartmentUpdateOne) ClearStudents() *DepartmentUpdateOne {
	duo.mutation.ClearStudents()
	return duo
}

// RemoveStudentIDs removes the "students" edge to Student entities by IDs.
func (duo *DepartmentUpdateOne) RemoveStudentIDs(ids ...int) *DepartmentUpdateOne {
	duo.mutation.RemoveStudentIDs(ids...)
	return duo
}

// RemoveStudents removes "students" edges to Student entities.
func (duo *DepartmentUpdateOne) RemoveStudents(s ...*Student) *DepartmentUpdateOne {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return duo.RemoveStudentIDs(ids...)
}

// Where appends a list predicates to the DepartmentUpdate builder.
func (duo *DepartmentUpdateOne) Where(ps ...predicate.Department) *DepartmentUpdateOne {
	duo.mutation.Where(ps...)
	return duo
}

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

// Save executes the query and returns the updated Department entity.
func (duo *DepartmentUpdateOne) Save(ctx context.Context) (*Department, error) {
	return withHooks(ctx, duo.sqlSave, duo.mutation, duo.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (duo *DepartmentUpdateOne) check() error {
	if v, ok := duo.mutation.Name(); ok {
		if err := department.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Department.name": %w`, err)}
		}
	}
	return nil
}

func (duo *DepartmentUpdateOne) sqlSave(ctx context.Context) (_node *Department, err error) {
	if err := duo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(department.Table, department.Columns, sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt))
	id, ok := duo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Department.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := duo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, department.FieldID)
		for _, f := range fields {
			if !department.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != department.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := duo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := duo.mutation.Name(); ok {
		_spec.SetField(department.FieldName, field.TypeString, value)
	}
	if duo.mutation.InstructorsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.InstructorsTable,
			Columns: []string{department.InstructorsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.RemovedInstructorsIDs(); len(nodes) > 0 && !duo.mutation.InstructorsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.InstructorsTable,
			Columns: []string{department.InstructorsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.InstructorsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.InstructorsTable,
			Columns: []string{department.InstructorsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if duo.mutation.CoursesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.CoursesTable,
			Columns: []string{department.CoursesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.RemovedCoursesIDs(); len(nodes) > 0 && !duo.mutation.CoursesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.CoursesTable,
			Columns: []string{department.CoursesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.CoursesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.CoursesTable,
			Columns: []string{department.CoursesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if duo.mutation.StudentsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.StudentsTable,
			Columns: []string{department.StudentsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.RemovedStudentsIDs(); len(nodes) > 0 && !duo.mutation.StudentsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.StudentsTable,
			Columns: []string{department.StudentsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := duo.mutation.StudentsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   department.StudentsTable,
			Columns: []string{department.StudentsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Department{config: duo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, duo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{department.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	duo.mutation.done = true
	return _node, nil
}
