user_update.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "code.osinet.fr/fgm/go__ent_demo/ent/car"
  8. "code.osinet.fr/fgm/go__ent_demo/ent/group"
  9. "code.osinet.fr/fgm/go__ent_demo/ent/predicate"
  10. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // UserUpdate is the builder for updating User entities.
  16. type UserUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *UserMutation
  20. }
  21. // Where appends a list predicates to the UserUpdate builder.
  22. func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
  23. uu.mutation.Where(ps...)
  24. return uu
  25. }
  26. // SetAge sets the "age" field.
  27. func (uu *UserUpdate) SetAge(i int) *UserUpdate {
  28. uu.mutation.ResetAge()
  29. uu.mutation.SetAge(i)
  30. return uu
  31. }
  32. // AddAge adds i to the "age" field.
  33. func (uu *UserUpdate) AddAge(i int) *UserUpdate {
  34. uu.mutation.AddAge(i)
  35. return uu
  36. }
  37. // SetName sets the "name" field.
  38. func (uu *UserUpdate) SetName(s string) *UserUpdate {
  39. uu.mutation.SetName(s)
  40. return uu
  41. }
  42. // SetNillableName sets the "name" field if the given value is not nil.
  43. func (uu *UserUpdate) SetNillableName(s *string) *UserUpdate {
  44. if s != nil {
  45. uu.SetName(*s)
  46. }
  47. return uu
  48. }
  49. // AddCarIDs adds the "cars" edge to the Car entity by IDs.
  50. func (uu *UserUpdate) AddCarIDs(ids ...int) *UserUpdate {
  51. uu.mutation.AddCarIDs(ids...)
  52. return uu
  53. }
  54. // AddCars adds the "cars" edges to the Car entity.
  55. func (uu *UserUpdate) AddCars(c ...*Car) *UserUpdate {
  56. ids := make([]int, len(c))
  57. for i := range c {
  58. ids[i] = c[i].ID
  59. }
  60. return uu.AddCarIDs(ids...)
  61. }
  62. // AddGroupIDs adds the "groups" edge to the Group entity by IDs.
  63. func (uu *UserUpdate) AddGroupIDs(ids ...int) *UserUpdate {
  64. uu.mutation.AddGroupIDs(ids...)
  65. return uu
  66. }
  67. // AddGroups adds the "groups" edges to the Group entity.
  68. func (uu *UserUpdate) AddGroups(g ...*Group) *UserUpdate {
  69. ids := make([]int, len(g))
  70. for i := range g {
  71. ids[i] = g[i].ID
  72. }
  73. return uu.AddGroupIDs(ids...)
  74. }
  75. // Mutation returns the UserMutation object of the builder.
  76. func (uu *UserUpdate) Mutation() *UserMutation {
  77. return uu.mutation
  78. }
  79. // ClearCars clears all "cars" edges to the Car entity.
  80. func (uu *UserUpdate) ClearCars() *UserUpdate {
  81. uu.mutation.ClearCars()
  82. return uu
  83. }
  84. // RemoveCarIDs removes the "cars" edge to Car entities by IDs.
  85. func (uu *UserUpdate) RemoveCarIDs(ids ...int) *UserUpdate {
  86. uu.mutation.RemoveCarIDs(ids...)
  87. return uu
  88. }
  89. // RemoveCars removes "cars" edges to Car entities.
  90. func (uu *UserUpdate) RemoveCars(c ...*Car) *UserUpdate {
  91. ids := make([]int, len(c))
  92. for i := range c {
  93. ids[i] = c[i].ID
  94. }
  95. return uu.RemoveCarIDs(ids...)
  96. }
  97. // ClearGroups clears all "groups" edges to the Group entity.
  98. func (uu *UserUpdate) ClearGroups() *UserUpdate {
  99. uu.mutation.ClearGroups()
  100. return uu
  101. }
  102. // RemoveGroupIDs removes the "groups" edge to Group entities by IDs.
  103. func (uu *UserUpdate) RemoveGroupIDs(ids ...int) *UserUpdate {
  104. uu.mutation.RemoveGroupIDs(ids...)
  105. return uu
  106. }
  107. // RemoveGroups removes "groups" edges to Group entities.
  108. func (uu *UserUpdate) RemoveGroups(g ...*Group) *UserUpdate {
  109. ids := make([]int, len(g))
  110. for i := range g {
  111. ids[i] = g[i].ID
  112. }
  113. return uu.RemoveGroupIDs(ids...)
  114. }
  115. // Save executes the query and returns the number of nodes affected by the update operation.
  116. func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
  117. var (
  118. err error
  119. affected int
  120. )
  121. if len(uu.hooks) == 0 {
  122. if err = uu.check(); err != nil {
  123. return 0, err
  124. }
  125. affected, err = uu.sqlSave(ctx)
  126. } else {
  127. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  128. mutation, ok := m.(*UserMutation)
  129. if !ok {
  130. return nil, fmt.Errorf("unexpected mutation type %T", m)
  131. }
  132. if err = uu.check(); err != nil {
  133. return 0, err
  134. }
  135. uu.mutation = mutation
  136. affected, err = uu.sqlSave(ctx)
  137. mutation.done = true
  138. return affected, err
  139. })
  140. for i := len(uu.hooks) - 1; i >= 0; i-- {
  141. if uu.hooks[i] == nil {
  142. return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  143. }
  144. mut = uu.hooks[i](mut)
  145. }
  146. if _, err := mut.Mutate(ctx, uu.mutation); err != nil {
  147. return 0, err
  148. }
  149. }
  150. return affected, err
  151. }
  152. // SaveX is like Save, but panics if an error occurs.
  153. func (uu *UserUpdate) SaveX(ctx context.Context) int {
  154. affected, err := uu.Save(ctx)
  155. if err != nil {
  156. panic(err)
  157. }
  158. return affected
  159. }
  160. // Exec executes the query.
  161. func (uu *UserUpdate) Exec(ctx context.Context) error {
  162. _, err := uu.Save(ctx)
  163. return err
  164. }
  165. // ExecX is like Exec, but panics if an error occurs.
  166. func (uu *UserUpdate) ExecX(ctx context.Context) {
  167. if err := uu.Exec(ctx); err != nil {
  168. panic(err)
  169. }
  170. }
  171. // check runs all checks and user-defined validators on the builder.
  172. func (uu *UserUpdate) check() error {
  173. if v, ok := uu.mutation.Age(); ok {
  174. if err := user.AgeValidator(v); err != nil {
  175. return &ValidationError{Name: "age", err: fmt.Errorf(`ent: validator failed for field "User.age": %w`, err)}
  176. }
  177. }
  178. return nil
  179. }
  180. func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
  181. _spec := &sqlgraph.UpdateSpec{
  182. Node: &sqlgraph.NodeSpec{
  183. Table: user.Table,
  184. Columns: user.Columns,
  185. ID: &sqlgraph.FieldSpec{
  186. Type: field.TypeInt,
  187. Column: user.FieldID,
  188. },
  189. },
  190. }
  191. if ps := uu.mutation.predicates; len(ps) > 0 {
  192. _spec.Predicate = func(selector *sql.Selector) {
  193. for i := range ps {
  194. ps[i](selector)
  195. }
  196. }
  197. }
  198. if value, ok := uu.mutation.Age(); ok {
  199. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  200. Type: field.TypeInt,
  201. Value: value,
  202. Column: user.FieldAge,
  203. })
  204. }
  205. if value, ok := uu.mutation.AddedAge(); ok {
  206. _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
  207. Type: field.TypeInt,
  208. Value: value,
  209. Column: user.FieldAge,
  210. })
  211. }
  212. if value, ok := uu.mutation.Name(); ok {
  213. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  214. Type: field.TypeString,
  215. Value: value,
  216. Column: user.FieldName,
  217. })
  218. }
  219. if uu.mutation.CarsCleared() {
  220. edge := &sqlgraph.EdgeSpec{
  221. Rel: sqlgraph.O2M,
  222. Inverse: false,
  223. Table: user.CarsTable,
  224. Columns: []string{user.CarsColumn},
  225. Bidi: false,
  226. Target: &sqlgraph.EdgeTarget{
  227. IDSpec: &sqlgraph.FieldSpec{
  228. Type: field.TypeInt,
  229. Column: car.FieldID,
  230. },
  231. },
  232. }
  233. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  234. }
  235. if nodes := uu.mutation.RemovedCarsIDs(); len(nodes) > 0 && !uu.mutation.CarsCleared() {
  236. edge := &sqlgraph.EdgeSpec{
  237. Rel: sqlgraph.O2M,
  238. Inverse: false,
  239. Table: user.CarsTable,
  240. Columns: []string{user.CarsColumn},
  241. Bidi: false,
  242. Target: &sqlgraph.EdgeTarget{
  243. IDSpec: &sqlgraph.FieldSpec{
  244. Type: field.TypeInt,
  245. Column: car.FieldID,
  246. },
  247. },
  248. }
  249. for _, k := range nodes {
  250. edge.Target.Nodes = append(edge.Target.Nodes, k)
  251. }
  252. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  253. }
  254. if nodes := uu.mutation.CarsIDs(); len(nodes) > 0 {
  255. edge := &sqlgraph.EdgeSpec{
  256. Rel: sqlgraph.O2M,
  257. Inverse: false,
  258. Table: user.CarsTable,
  259. Columns: []string{user.CarsColumn},
  260. Bidi: false,
  261. Target: &sqlgraph.EdgeTarget{
  262. IDSpec: &sqlgraph.FieldSpec{
  263. Type: field.TypeInt,
  264. Column: car.FieldID,
  265. },
  266. },
  267. }
  268. for _, k := range nodes {
  269. edge.Target.Nodes = append(edge.Target.Nodes, k)
  270. }
  271. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  272. }
  273. if uu.mutation.GroupsCleared() {
  274. edge := &sqlgraph.EdgeSpec{
  275. Rel: sqlgraph.M2M,
  276. Inverse: true,
  277. Table: user.GroupsTable,
  278. Columns: user.GroupsPrimaryKey,
  279. Bidi: false,
  280. Target: &sqlgraph.EdgeTarget{
  281. IDSpec: &sqlgraph.FieldSpec{
  282. Type: field.TypeInt,
  283. Column: group.FieldID,
  284. },
  285. },
  286. }
  287. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  288. }
  289. if nodes := uu.mutation.RemovedGroupsIDs(); len(nodes) > 0 && !uu.mutation.GroupsCleared() {
  290. edge := &sqlgraph.EdgeSpec{
  291. Rel: sqlgraph.M2M,
  292. Inverse: true,
  293. Table: user.GroupsTable,
  294. Columns: user.GroupsPrimaryKey,
  295. Bidi: false,
  296. Target: &sqlgraph.EdgeTarget{
  297. IDSpec: &sqlgraph.FieldSpec{
  298. Type: field.TypeInt,
  299. Column: group.FieldID,
  300. },
  301. },
  302. }
  303. for _, k := range nodes {
  304. edge.Target.Nodes = append(edge.Target.Nodes, k)
  305. }
  306. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  307. }
  308. if nodes := uu.mutation.GroupsIDs(); len(nodes) > 0 {
  309. edge := &sqlgraph.EdgeSpec{
  310. Rel: sqlgraph.M2M,
  311. Inverse: true,
  312. Table: user.GroupsTable,
  313. Columns: user.GroupsPrimaryKey,
  314. Bidi: false,
  315. Target: &sqlgraph.EdgeTarget{
  316. IDSpec: &sqlgraph.FieldSpec{
  317. Type: field.TypeInt,
  318. Column: group.FieldID,
  319. },
  320. },
  321. }
  322. for _, k := range nodes {
  323. edge.Target.Nodes = append(edge.Target.Nodes, k)
  324. }
  325. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  326. }
  327. if n, err = sqlgraph.UpdateNodes(ctx, uu.driver, _spec); err != nil {
  328. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  329. err = &NotFoundError{user.Label}
  330. } else if sqlgraph.IsConstraintError(err) {
  331. err = &ConstraintError{err.Error(), err}
  332. }
  333. return 0, err
  334. }
  335. return n, nil
  336. }
  337. // UserUpdateOne is the builder for updating a single User entity.
  338. type UserUpdateOne struct {
  339. config
  340. fields []string
  341. hooks []Hook
  342. mutation *UserMutation
  343. }
  344. // SetAge sets the "age" field.
  345. func (uuo *UserUpdateOne) SetAge(i int) *UserUpdateOne {
  346. uuo.mutation.ResetAge()
  347. uuo.mutation.SetAge(i)
  348. return uuo
  349. }
  350. // AddAge adds i to the "age" field.
  351. func (uuo *UserUpdateOne) AddAge(i int) *UserUpdateOne {
  352. uuo.mutation.AddAge(i)
  353. return uuo
  354. }
  355. // SetName sets the "name" field.
  356. func (uuo *UserUpdateOne) SetName(s string) *UserUpdateOne {
  357. uuo.mutation.SetName(s)
  358. return uuo
  359. }
  360. // SetNillableName sets the "name" field if the given value is not nil.
  361. func (uuo *UserUpdateOne) SetNillableName(s *string) *UserUpdateOne {
  362. if s != nil {
  363. uuo.SetName(*s)
  364. }
  365. return uuo
  366. }
  367. // AddCarIDs adds the "cars" edge to the Car entity by IDs.
  368. func (uuo *UserUpdateOne) AddCarIDs(ids ...int) *UserUpdateOne {
  369. uuo.mutation.AddCarIDs(ids...)
  370. return uuo
  371. }
  372. // AddCars adds the "cars" edges to the Car entity.
  373. func (uuo *UserUpdateOne) AddCars(c ...*Car) *UserUpdateOne {
  374. ids := make([]int, len(c))
  375. for i := range c {
  376. ids[i] = c[i].ID
  377. }
  378. return uuo.AddCarIDs(ids...)
  379. }
  380. // AddGroupIDs adds the "groups" edge to the Group entity by IDs.
  381. func (uuo *UserUpdateOne) AddGroupIDs(ids ...int) *UserUpdateOne {
  382. uuo.mutation.AddGroupIDs(ids...)
  383. return uuo
  384. }
  385. // AddGroups adds the "groups" edges to the Group entity.
  386. func (uuo *UserUpdateOne) AddGroups(g ...*Group) *UserUpdateOne {
  387. ids := make([]int, len(g))
  388. for i := range g {
  389. ids[i] = g[i].ID
  390. }
  391. return uuo.AddGroupIDs(ids...)
  392. }
  393. // Mutation returns the UserMutation object of the builder.
  394. func (uuo *UserUpdateOne) Mutation() *UserMutation {
  395. return uuo.mutation
  396. }
  397. // ClearCars clears all "cars" edges to the Car entity.
  398. func (uuo *UserUpdateOne) ClearCars() *UserUpdateOne {
  399. uuo.mutation.ClearCars()
  400. return uuo
  401. }
  402. // RemoveCarIDs removes the "cars" edge to Car entities by IDs.
  403. func (uuo *UserUpdateOne) RemoveCarIDs(ids ...int) *UserUpdateOne {
  404. uuo.mutation.RemoveCarIDs(ids...)
  405. return uuo
  406. }
  407. // RemoveCars removes "cars" edges to Car entities.
  408. func (uuo *UserUpdateOne) RemoveCars(c ...*Car) *UserUpdateOne {
  409. ids := make([]int, len(c))
  410. for i := range c {
  411. ids[i] = c[i].ID
  412. }
  413. return uuo.RemoveCarIDs(ids...)
  414. }
  415. // ClearGroups clears all "groups" edges to the Group entity.
  416. func (uuo *UserUpdateOne) ClearGroups() *UserUpdateOne {
  417. uuo.mutation.ClearGroups()
  418. return uuo
  419. }
  420. // RemoveGroupIDs removes the "groups" edge to Group entities by IDs.
  421. func (uuo *UserUpdateOne) RemoveGroupIDs(ids ...int) *UserUpdateOne {
  422. uuo.mutation.RemoveGroupIDs(ids...)
  423. return uuo
  424. }
  425. // RemoveGroups removes "groups" edges to Group entities.
  426. func (uuo *UserUpdateOne) RemoveGroups(g ...*Group) *UserUpdateOne {
  427. ids := make([]int, len(g))
  428. for i := range g {
  429. ids[i] = g[i].ID
  430. }
  431. return uuo.RemoveGroupIDs(ids...)
  432. }
  433. // Select allows selecting one or more fields (columns) of the returned entity.
  434. // The default is selecting all fields defined in the entity schema.
  435. func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne {
  436. uuo.fields = append([]string{field}, fields...)
  437. return uuo
  438. }
  439. // Save executes the query and returns the updated User entity.
  440. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
  441. var (
  442. err error
  443. node *User
  444. )
  445. if len(uuo.hooks) == 0 {
  446. if err = uuo.check(); err != nil {
  447. return nil, err
  448. }
  449. node, err = uuo.sqlSave(ctx)
  450. } else {
  451. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  452. mutation, ok := m.(*UserMutation)
  453. if !ok {
  454. return nil, fmt.Errorf("unexpected mutation type %T", m)
  455. }
  456. if err = uuo.check(); err != nil {
  457. return nil, err
  458. }
  459. uuo.mutation = mutation
  460. node, err = uuo.sqlSave(ctx)
  461. mutation.done = true
  462. return node, err
  463. })
  464. for i := len(uuo.hooks) - 1; i >= 0; i-- {
  465. if uuo.hooks[i] == nil {
  466. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  467. }
  468. mut = uuo.hooks[i](mut)
  469. }
  470. if _, err := mut.Mutate(ctx, uuo.mutation); err != nil {
  471. return nil, err
  472. }
  473. }
  474. return node, err
  475. }
  476. // SaveX is like Save, but panics if an error occurs.
  477. func (uuo *UserUpdateOne) SaveX(ctx context.Context) *User {
  478. node, err := uuo.Save(ctx)
  479. if err != nil {
  480. panic(err)
  481. }
  482. return node
  483. }
  484. // Exec executes the query on the entity.
  485. func (uuo *UserUpdateOne) Exec(ctx context.Context) error {
  486. _, err := uuo.Save(ctx)
  487. return err
  488. }
  489. // ExecX is like Exec, but panics if an error occurs.
  490. func (uuo *UserUpdateOne) ExecX(ctx context.Context) {
  491. if err := uuo.Exec(ctx); err != nil {
  492. panic(err)
  493. }
  494. }
  495. // check runs all checks and user-defined validators on the builder.
  496. func (uuo *UserUpdateOne) check() error {
  497. if v, ok := uuo.mutation.Age(); ok {
  498. if err := user.AgeValidator(v); err != nil {
  499. return &ValidationError{Name: "age", err: fmt.Errorf(`ent: validator failed for field "User.age": %w`, err)}
  500. }
  501. }
  502. return nil
  503. }
  504. func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
  505. _spec := &sqlgraph.UpdateSpec{
  506. Node: &sqlgraph.NodeSpec{
  507. Table: user.Table,
  508. Columns: user.Columns,
  509. ID: &sqlgraph.FieldSpec{
  510. Type: field.TypeInt,
  511. Column: user.FieldID,
  512. },
  513. },
  514. }
  515. id, ok := uuo.mutation.ID()
  516. if !ok {
  517. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "User.id" for update`)}
  518. }
  519. _spec.Node.ID.Value = id
  520. if fields := uuo.fields; len(fields) > 0 {
  521. _spec.Node.Columns = make([]string, 0, len(fields))
  522. _spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
  523. for _, f := range fields {
  524. if !user.ValidColumn(f) {
  525. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  526. }
  527. if f != user.FieldID {
  528. _spec.Node.Columns = append(_spec.Node.Columns, f)
  529. }
  530. }
  531. }
  532. if ps := uuo.mutation.predicates; len(ps) > 0 {
  533. _spec.Predicate = func(selector *sql.Selector) {
  534. for i := range ps {
  535. ps[i](selector)
  536. }
  537. }
  538. }
  539. if value, ok := uuo.mutation.Age(); ok {
  540. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  541. Type: field.TypeInt,
  542. Value: value,
  543. Column: user.FieldAge,
  544. })
  545. }
  546. if value, ok := uuo.mutation.AddedAge(); ok {
  547. _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
  548. Type: field.TypeInt,
  549. Value: value,
  550. Column: user.FieldAge,
  551. })
  552. }
  553. if value, ok := uuo.mutation.Name(); ok {
  554. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  555. Type: field.TypeString,
  556. Value: value,
  557. Column: user.FieldName,
  558. })
  559. }
  560. if uuo.mutation.CarsCleared() {
  561. edge := &sqlgraph.EdgeSpec{
  562. Rel: sqlgraph.O2M,
  563. Inverse: false,
  564. Table: user.CarsTable,
  565. Columns: []string{user.CarsColumn},
  566. Bidi: false,
  567. Target: &sqlgraph.EdgeTarget{
  568. IDSpec: &sqlgraph.FieldSpec{
  569. Type: field.TypeInt,
  570. Column: car.FieldID,
  571. },
  572. },
  573. }
  574. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  575. }
  576. if nodes := uuo.mutation.RemovedCarsIDs(); len(nodes) > 0 && !uuo.mutation.CarsCleared() {
  577. edge := &sqlgraph.EdgeSpec{
  578. Rel: sqlgraph.O2M,
  579. Inverse: false,
  580. Table: user.CarsTable,
  581. Columns: []string{user.CarsColumn},
  582. Bidi: false,
  583. Target: &sqlgraph.EdgeTarget{
  584. IDSpec: &sqlgraph.FieldSpec{
  585. Type: field.TypeInt,
  586. Column: car.FieldID,
  587. },
  588. },
  589. }
  590. for _, k := range nodes {
  591. edge.Target.Nodes = append(edge.Target.Nodes, k)
  592. }
  593. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  594. }
  595. if nodes := uuo.mutation.CarsIDs(); len(nodes) > 0 {
  596. edge := &sqlgraph.EdgeSpec{
  597. Rel: sqlgraph.O2M,
  598. Inverse: false,
  599. Table: user.CarsTable,
  600. Columns: []string{user.CarsColumn},
  601. Bidi: false,
  602. Target: &sqlgraph.EdgeTarget{
  603. IDSpec: &sqlgraph.FieldSpec{
  604. Type: field.TypeInt,
  605. Column: car.FieldID,
  606. },
  607. },
  608. }
  609. for _, k := range nodes {
  610. edge.Target.Nodes = append(edge.Target.Nodes, k)
  611. }
  612. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  613. }
  614. if uuo.mutation.GroupsCleared() {
  615. edge := &sqlgraph.EdgeSpec{
  616. Rel: sqlgraph.M2M,
  617. Inverse: true,
  618. Table: user.GroupsTable,
  619. Columns: user.GroupsPrimaryKey,
  620. Bidi: false,
  621. Target: &sqlgraph.EdgeTarget{
  622. IDSpec: &sqlgraph.FieldSpec{
  623. Type: field.TypeInt,
  624. Column: group.FieldID,
  625. },
  626. },
  627. }
  628. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  629. }
  630. if nodes := uuo.mutation.RemovedGroupsIDs(); len(nodes) > 0 && !uuo.mutation.GroupsCleared() {
  631. edge := &sqlgraph.EdgeSpec{
  632. Rel: sqlgraph.M2M,
  633. Inverse: true,
  634. Table: user.GroupsTable,
  635. Columns: user.GroupsPrimaryKey,
  636. Bidi: false,
  637. Target: &sqlgraph.EdgeTarget{
  638. IDSpec: &sqlgraph.FieldSpec{
  639. Type: field.TypeInt,
  640. Column: group.FieldID,
  641. },
  642. },
  643. }
  644. for _, k := range nodes {
  645. edge.Target.Nodes = append(edge.Target.Nodes, k)
  646. }
  647. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  648. }
  649. if nodes := uuo.mutation.GroupsIDs(); len(nodes) > 0 {
  650. edge := &sqlgraph.EdgeSpec{
  651. Rel: sqlgraph.M2M,
  652. Inverse: true,
  653. Table: user.GroupsTable,
  654. Columns: user.GroupsPrimaryKey,
  655. Bidi: false,
  656. Target: &sqlgraph.EdgeTarget{
  657. IDSpec: &sqlgraph.FieldSpec{
  658. Type: field.TypeInt,
  659. Column: group.FieldID,
  660. },
  661. },
  662. }
  663. for _, k := range nodes {
  664. edge.Target.Nodes = append(edge.Target.Nodes, k)
  665. }
  666. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  667. }
  668. _node = &User{config: uuo.config}
  669. _spec.Assign = _node.assignValues
  670. _spec.ScanValues = _node.scanValues
  671. if err = sqlgraph.UpdateNode(ctx, uuo.driver, _spec); err != nil {
  672. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  673. err = &NotFoundError{user.Label}
  674. } else if sqlgraph.IsConstraintError(err) {
  675. err = &ConstraintError{err.Error(), err}
  676. }
  677. return nil, err
  678. }
  679. return _node, nil
  680. }