labelrelationship_update.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "wechat-api/ent/contact"
  9. "wechat-api/ent/label"
  10. "wechat-api/ent/labelrelationship"
  11. "wechat-api/ent/predicate"
  12. "entgo.io/ent/dialect/sql"
  13. "entgo.io/ent/dialect/sql/sqlgraph"
  14. "entgo.io/ent/schema/field"
  15. )
  16. // LabelRelationshipUpdate is the builder for updating LabelRelationship entities.
  17. type LabelRelationshipUpdate struct {
  18. config
  19. hooks []Hook
  20. mutation *LabelRelationshipMutation
  21. }
  22. // Where appends a list predicates to the LabelRelationshipUpdate builder.
  23. func (lru *LabelRelationshipUpdate) Where(ps ...predicate.LabelRelationship) *LabelRelationshipUpdate {
  24. lru.mutation.Where(ps...)
  25. return lru
  26. }
  27. // SetUpdatedAt sets the "updated_at" field.
  28. func (lru *LabelRelationshipUpdate) SetUpdatedAt(t time.Time) *LabelRelationshipUpdate {
  29. lru.mutation.SetUpdatedAt(t)
  30. return lru
  31. }
  32. // SetStatus sets the "status" field.
  33. func (lru *LabelRelationshipUpdate) SetStatus(u uint8) *LabelRelationshipUpdate {
  34. lru.mutation.ResetStatus()
  35. lru.mutation.SetStatus(u)
  36. return lru
  37. }
  38. // SetNillableStatus sets the "status" field if the given value is not nil.
  39. func (lru *LabelRelationshipUpdate) SetNillableStatus(u *uint8) *LabelRelationshipUpdate {
  40. if u != nil {
  41. lru.SetStatus(*u)
  42. }
  43. return lru
  44. }
  45. // AddStatus adds u to the "status" field.
  46. func (lru *LabelRelationshipUpdate) AddStatus(u int8) *LabelRelationshipUpdate {
  47. lru.mutation.AddStatus(u)
  48. return lru
  49. }
  50. // ClearStatus clears the value of the "status" field.
  51. func (lru *LabelRelationshipUpdate) ClearStatus() *LabelRelationshipUpdate {
  52. lru.mutation.ClearStatus()
  53. return lru
  54. }
  55. // SetDeletedAt sets the "deleted_at" field.
  56. func (lru *LabelRelationshipUpdate) SetDeletedAt(t time.Time) *LabelRelationshipUpdate {
  57. lru.mutation.SetDeletedAt(t)
  58. return lru
  59. }
  60. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  61. func (lru *LabelRelationshipUpdate) SetNillableDeletedAt(t *time.Time) *LabelRelationshipUpdate {
  62. if t != nil {
  63. lru.SetDeletedAt(*t)
  64. }
  65. return lru
  66. }
  67. // ClearDeletedAt clears the value of the "deleted_at" field.
  68. func (lru *LabelRelationshipUpdate) ClearDeletedAt() *LabelRelationshipUpdate {
  69. lru.mutation.ClearDeletedAt()
  70. return lru
  71. }
  72. // SetLabelID sets the "label_id" field.
  73. func (lru *LabelRelationshipUpdate) SetLabelID(u uint64) *LabelRelationshipUpdate {
  74. lru.mutation.SetLabelID(u)
  75. return lru
  76. }
  77. // SetNillableLabelID sets the "label_id" field if the given value is not nil.
  78. func (lru *LabelRelationshipUpdate) SetNillableLabelID(u *uint64) *LabelRelationshipUpdate {
  79. if u != nil {
  80. lru.SetLabelID(*u)
  81. }
  82. return lru
  83. }
  84. // SetContactID sets the "contact_id" field.
  85. func (lru *LabelRelationshipUpdate) SetContactID(u uint64) *LabelRelationshipUpdate {
  86. lru.mutation.SetContactID(u)
  87. return lru
  88. }
  89. // SetNillableContactID sets the "contact_id" field if the given value is not nil.
  90. func (lru *LabelRelationshipUpdate) SetNillableContactID(u *uint64) *LabelRelationshipUpdate {
  91. if u != nil {
  92. lru.SetContactID(*u)
  93. }
  94. return lru
  95. }
  96. // SetOrganizationID sets the "organization_id" field.
  97. func (lru *LabelRelationshipUpdate) SetOrganizationID(u uint64) *LabelRelationshipUpdate {
  98. lru.mutation.ResetOrganizationID()
  99. lru.mutation.SetOrganizationID(u)
  100. return lru
  101. }
  102. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  103. func (lru *LabelRelationshipUpdate) SetNillableOrganizationID(u *uint64) *LabelRelationshipUpdate {
  104. if u != nil {
  105. lru.SetOrganizationID(*u)
  106. }
  107. return lru
  108. }
  109. // AddOrganizationID adds u to the "organization_id" field.
  110. func (lru *LabelRelationshipUpdate) AddOrganizationID(u int64) *LabelRelationshipUpdate {
  111. lru.mutation.AddOrganizationID(u)
  112. return lru
  113. }
  114. // ClearOrganizationID clears the value of the "organization_id" field.
  115. func (lru *LabelRelationshipUpdate) ClearOrganizationID() *LabelRelationshipUpdate {
  116. lru.mutation.ClearOrganizationID()
  117. return lru
  118. }
  119. // SetContactsID sets the "contacts" edge to the Contact entity by ID.
  120. func (lru *LabelRelationshipUpdate) SetContactsID(id uint64) *LabelRelationshipUpdate {
  121. lru.mutation.SetContactsID(id)
  122. return lru
  123. }
  124. // SetContacts sets the "contacts" edge to the Contact entity.
  125. func (lru *LabelRelationshipUpdate) SetContacts(c *Contact) *LabelRelationshipUpdate {
  126. return lru.SetContactsID(c.ID)
  127. }
  128. // SetLabelsID sets the "labels" edge to the Label entity by ID.
  129. func (lru *LabelRelationshipUpdate) SetLabelsID(id uint64) *LabelRelationshipUpdate {
  130. lru.mutation.SetLabelsID(id)
  131. return lru
  132. }
  133. // SetLabels sets the "labels" edge to the Label entity.
  134. func (lru *LabelRelationshipUpdate) SetLabels(l *Label) *LabelRelationshipUpdate {
  135. return lru.SetLabelsID(l.ID)
  136. }
  137. // Mutation returns the LabelRelationshipMutation object of the builder.
  138. func (lru *LabelRelationshipUpdate) Mutation() *LabelRelationshipMutation {
  139. return lru.mutation
  140. }
  141. // ClearContacts clears the "contacts" edge to the Contact entity.
  142. func (lru *LabelRelationshipUpdate) ClearContacts() *LabelRelationshipUpdate {
  143. lru.mutation.ClearContacts()
  144. return lru
  145. }
  146. // ClearLabels clears the "labels" edge to the Label entity.
  147. func (lru *LabelRelationshipUpdate) ClearLabels() *LabelRelationshipUpdate {
  148. lru.mutation.ClearLabels()
  149. return lru
  150. }
  151. // Save executes the query and returns the number of nodes affected by the update operation.
  152. func (lru *LabelRelationshipUpdate) Save(ctx context.Context) (int, error) {
  153. if err := lru.defaults(); err != nil {
  154. return 0, err
  155. }
  156. return withHooks(ctx, lru.sqlSave, lru.mutation, lru.hooks)
  157. }
  158. // SaveX is like Save, but panics if an error occurs.
  159. func (lru *LabelRelationshipUpdate) SaveX(ctx context.Context) int {
  160. affected, err := lru.Save(ctx)
  161. if err != nil {
  162. panic(err)
  163. }
  164. return affected
  165. }
  166. // Exec executes the query.
  167. func (lru *LabelRelationshipUpdate) Exec(ctx context.Context) error {
  168. _, err := lru.Save(ctx)
  169. return err
  170. }
  171. // ExecX is like Exec, but panics if an error occurs.
  172. func (lru *LabelRelationshipUpdate) ExecX(ctx context.Context) {
  173. if err := lru.Exec(ctx); err != nil {
  174. panic(err)
  175. }
  176. }
  177. // defaults sets the default values of the builder before save.
  178. func (lru *LabelRelationshipUpdate) defaults() error {
  179. if _, ok := lru.mutation.UpdatedAt(); !ok {
  180. if labelrelationship.UpdateDefaultUpdatedAt == nil {
  181. return fmt.Errorf("ent: uninitialized labelrelationship.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  182. }
  183. v := labelrelationship.UpdateDefaultUpdatedAt()
  184. lru.mutation.SetUpdatedAt(v)
  185. }
  186. return nil
  187. }
  188. // check runs all checks and user-defined validators on the builder.
  189. func (lru *LabelRelationshipUpdate) check() error {
  190. if _, ok := lru.mutation.ContactsID(); lru.mutation.ContactsCleared() && !ok {
  191. return errors.New(`ent: clearing a required unique edge "LabelRelationship.contacts"`)
  192. }
  193. if _, ok := lru.mutation.LabelsID(); lru.mutation.LabelsCleared() && !ok {
  194. return errors.New(`ent: clearing a required unique edge "LabelRelationship.labels"`)
  195. }
  196. return nil
  197. }
  198. func (lru *LabelRelationshipUpdate) sqlSave(ctx context.Context) (n int, err error) {
  199. if err := lru.check(); err != nil {
  200. return n, err
  201. }
  202. _spec := sqlgraph.NewUpdateSpec(labelrelationship.Table, labelrelationship.Columns, sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64))
  203. if ps := lru.mutation.predicates; len(ps) > 0 {
  204. _spec.Predicate = func(selector *sql.Selector) {
  205. for i := range ps {
  206. ps[i](selector)
  207. }
  208. }
  209. }
  210. if value, ok := lru.mutation.UpdatedAt(); ok {
  211. _spec.SetField(labelrelationship.FieldUpdatedAt, field.TypeTime, value)
  212. }
  213. if value, ok := lru.mutation.Status(); ok {
  214. _spec.SetField(labelrelationship.FieldStatus, field.TypeUint8, value)
  215. }
  216. if value, ok := lru.mutation.AddedStatus(); ok {
  217. _spec.AddField(labelrelationship.FieldStatus, field.TypeUint8, value)
  218. }
  219. if lru.mutation.StatusCleared() {
  220. _spec.ClearField(labelrelationship.FieldStatus, field.TypeUint8)
  221. }
  222. if value, ok := lru.mutation.DeletedAt(); ok {
  223. _spec.SetField(labelrelationship.FieldDeletedAt, field.TypeTime, value)
  224. }
  225. if lru.mutation.DeletedAtCleared() {
  226. _spec.ClearField(labelrelationship.FieldDeletedAt, field.TypeTime)
  227. }
  228. if value, ok := lru.mutation.OrganizationID(); ok {
  229. _spec.SetField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
  230. }
  231. if value, ok := lru.mutation.AddedOrganizationID(); ok {
  232. _spec.AddField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
  233. }
  234. if lru.mutation.OrganizationIDCleared() {
  235. _spec.ClearField(labelrelationship.FieldOrganizationID, field.TypeUint64)
  236. }
  237. if lru.mutation.ContactsCleared() {
  238. edge := &sqlgraph.EdgeSpec{
  239. Rel: sqlgraph.M2O,
  240. Inverse: true,
  241. Table: labelrelationship.ContactsTable,
  242. Columns: []string{labelrelationship.ContactsColumn},
  243. Bidi: false,
  244. Target: &sqlgraph.EdgeTarget{
  245. IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
  246. },
  247. }
  248. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  249. }
  250. if nodes := lru.mutation.ContactsIDs(); len(nodes) > 0 {
  251. edge := &sqlgraph.EdgeSpec{
  252. Rel: sqlgraph.M2O,
  253. Inverse: true,
  254. Table: labelrelationship.ContactsTable,
  255. Columns: []string{labelrelationship.ContactsColumn},
  256. Bidi: false,
  257. Target: &sqlgraph.EdgeTarget{
  258. IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
  259. },
  260. }
  261. for _, k := range nodes {
  262. edge.Target.Nodes = append(edge.Target.Nodes, k)
  263. }
  264. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  265. }
  266. if lru.mutation.LabelsCleared() {
  267. edge := &sqlgraph.EdgeSpec{
  268. Rel: sqlgraph.M2O,
  269. Inverse: true,
  270. Table: labelrelationship.LabelsTable,
  271. Columns: []string{labelrelationship.LabelsColumn},
  272. Bidi: false,
  273. Target: &sqlgraph.EdgeTarget{
  274. IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
  275. },
  276. }
  277. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  278. }
  279. if nodes := lru.mutation.LabelsIDs(); len(nodes) > 0 {
  280. edge := &sqlgraph.EdgeSpec{
  281. Rel: sqlgraph.M2O,
  282. Inverse: true,
  283. Table: labelrelationship.LabelsTable,
  284. Columns: []string{labelrelationship.LabelsColumn},
  285. Bidi: false,
  286. Target: &sqlgraph.EdgeTarget{
  287. IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
  288. },
  289. }
  290. for _, k := range nodes {
  291. edge.Target.Nodes = append(edge.Target.Nodes, k)
  292. }
  293. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  294. }
  295. if n, err = sqlgraph.UpdateNodes(ctx, lru.driver, _spec); err != nil {
  296. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  297. err = &NotFoundError{labelrelationship.Label}
  298. } else if sqlgraph.IsConstraintError(err) {
  299. err = &ConstraintError{msg: err.Error(), wrap: err}
  300. }
  301. return 0, err
  302. }
  303. lru.mutation.done = true
  304. return n, nil
  305. }
  306. // LabelRelationshipUpdateOne is the builder for updating a single LabelRelationship entity.
  307. type LabelRelationshipUpdateOne struct {
  308. config
  309. fields []string
  310. hooks []Hook
  311. mutation *LabelRelationshipMutation
  312. }
  313. // SetUpdatedAt sets the "updated_at" field.
  314. func (lruo *LabelRelationshipUpdateOne) SetUpdatedAt(t time.Time) *LabelRelationshipUpdateOne {
  315. lruo.mutation.SetUpdatedAt(t)
  316. return lruo
  317. }
  318. // SetStatus sets the "status" field.
  319. func (lruo *LabelRelationshipUpdateOne) SetStatus(u uint8) *LabelRelationshipUpdateOne {
  320. lruo.mutation.ResetStatus()
  321. lruo.mutation.SetStatus(u)
  322. return lruo
  323. }
  324. // SetNillableStatus sets the "status" field if the given value is not nil.
  325. func (lruo *LabelRelationshipUpdateOne) SetNillableStatus(u *uint8) *LabelRelationshipUpdateOne {
  326. if u != nil {
  327. lruo.SetStatus(*u)
  328. }
  329. return lruo
  330. }
  331. // AddStatus adds u to the "status" field.
  332. func (lruo *LabelRelationshipUpdateOne) AddStatus(u int8) *LabelRelationshipUpdateOne {
  333. lruo.mutation.AddStatus(u)
  334. return lruo
  335. }
  336. // ClearStatus clears the value of the "status" field.
  337. func (lruo *LabelRelationshipUpdateOne) ClearStatus() *LabelRelationshipUpdateOne {
  338. lruo.mutation.ClearStatus()
  339. return lruo
  340. }
  341. // SetDeletedAt sets the "deleted_at" field.
  342. func (lruo *LabelRelationshipUpdateOne) SetDeletedAt(t time.Time) *LabelRelationshipUpdateOne {
  343. lruo.mutation.SetDeletedAt(t)
  344. return lruo
  345. }
  346. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  347. func (lruo *LabelRelationshipUpdateOne) SetNillableDeletedAt(t *time.Time) *LabelRelationshipUpdateOne {
  348. if t != nil {
  349. lruo.SetDeletedAt(*t)
  350. }
  351. return lruo
  352. }
  353. // ClearDeletedAt clears the value of the "deleted_at" field.
  354. func (lruo *LabelRelationshipUpdateOne) ClearDeletedAt() *LabelRelationshipUpdateOne {
  355. lruo.mutation.ClearDeletedAt()
  356. return lruo
  357. }
  358. // SetLabelID sets the "label_id" field.
  359. func (lruo *LabelRelationshipUpdateOne) SetLabelID(u uint64) *LabelRelationshipUpdateOne {
  360. lruo.mutation.SetLabelID(u)
  361. return lruo
  362. }
  363. // SetNillableLabelID sets the "label_id" field if the given value is not nil.
  364. func (lruo *LabelRelationshipUpdateOne) SetNillableLabelID(u *uint64) *LabelRelationshipUpdateOne {
  365. if u != nil {
  366. lruo.SetLabelID(*u)
  367. }
  368. return lruo
  369. }
  370. // SetContactID sets the "contact_id" field.
  371. func (lruo *LabelRelationshipUpdateOne) SetContactID(u uint64) *LabelRelationshipUpdateOne {
  372. lruo.mutation.SetContactID(u)
  373. return lruo
  374. }
  375. // SetNillableContactID sets the "contact_id" field if the given value is not nil.
  376. func (lruo *LabelRelationshipUpdateOne) SetNillableContactID(u *uint64) *LabelRelationshipUpdateOne {
  377. if u != nil {
  378. lruo.SetContactID(*u)
  379. }
  380. return lruo
  381. }
  382. // SetOrganizationID sets the "organization_id" field.
  383. func (lruo *LabelRelationshipUpdateOne) SetOrganizationID(u uint64) *LabelRelationshipUpdateOne {
  384. lruo.mutation.ResetOrganizationID()
  385. lruo.mutation.SetOrganizationID(u)
  386. return lruo
  387. }
  388. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  389. func (lruo *LabelRelationshipUpdateOne) SetNillableOrganizationID(u *uint64) *LabelRelationshipUpdateOne {
  390. if u != nil {
  391. lruo.SetOrganizationID(*u)
  392. }
  393. return lruo
  394. }
  395. // AddOrganizationID adds u to the "organization_id" field.
  396. func (lruo *LabelRelationshipUpdateOne) AddOrganizationID(u int64) *LabelRelationshipUpdateOne {
  397. lruo.mutation.AddOrganizationID(u)
  398. return lruo
  399. }
  400. // ClearOrganizationID clears the value of the "organization_id" field.
  401. func (lruo *LabelRelationshipUpdateOne) ClearOrganizationID() *LabelRelationshipUpdateOne {
  402. lruo.mutation.ClearOrganizationID()
  403. return lruo
  404. }
  405. // SetContactsID sets the "contacts" edge to the Contact entity by ID.
  406. func (lruo *LabelRelationshipUpdateOne) SetContactsID(id uint64) *LabelRelationshipUpdateOne {
  407. lruo.mutation.SetContactsID(id)
  408. return lruo
  409. }
  410. // SetContacts sets the "contacts" edge to the Contact entity.
  411. func (lruo *LabelRelationshipUpdateOne) SetContacts(c *Contact) *LabelRelationshipUpdateOne {
  412. return lruo.SetContactsID(c.ID)
  413. }
  414. // SetLabelsID sets the "labels" edge to the Label entity by ID.
  415. func (lruo *LabelRelationshipUpdateOne) SetLabelsID(id uint64) *LabelRelationshipUpdateOne {
  416. lruo.mutation.SetLabelsID(id)
  417. return lruo
  418. }
  419. // SetLabels sets the "labels" edge to the Label entity.
  420. func (lruo *LabelRelationshipUpdateOne) SetLabels(l *Label) *LabelRelationshipUpdateOne {
  421. return lruo.SetLabelsID(l.ID)
  422. }
  423. // Mutation returns the LabelRelationshipMutation object of the builder.
  424. func (lruo *LabelRelationshipUpdateOne) Mutation() *LabelRelationshipMutation {
  425. return lruo.mutation
  426. }
  427. // ClearContacts clears the "contacts" edge to the Contact entity.
  428. func (lruo *LabelRelationshipUpdateOne) ClearContacts() *LabelRelationshipUpdateOne {
  429. lruo.mutation.ClearContacts()
  430. return lruo
  431. }
  432. // ClearLabels clears the "labels" edge to the Label entity.
  433. func (lruo *LabelRelationshipUpdateOne) ClearLabels() *LabelRelationshipUpdateOne {
  434. lruo.mutation.ClearLabels()
  435. return lruo
  436. }
  437. // Where appends a list predicates to the LabelRelationshipUpdate builder.
  438. func (lruo *LabelRelationshipUpdateOne) Where(ps ...predicate.LabelRelationship) *LabelRelationshipUpdateOne {
  439. lruo.mutation.Where(ps...)
  440. return lruo
  441. }
  442. // Select allows selecting one or more fields (columns) of the returned entity.
  443. // The default is selecting all fields defined in the entity schema.
  444. func (lruo *LabelRelationshipUpdateOne) Select(field string, fields ...string) *LabelRelationshipUpdateOne {
  445. lruo.fields = append([]string{field}, fields...)
  446. return lruo
  447. }
  448. // Save executes the query and returns the updated LabelRelationship entity.
  449. func (lruo *LabelRelationshipUpdateOne) Save(ctx context.Context) (*LabelRelationship, error) {
  450. if err := lruo.defaults(); err != nil {
  451. return nil, err
  452. }
  453. return withHooks(ctx, lruo.sqlSave, lruo.mutation, lruo.hooks)
  454. }
  455. // SaveX is like Save, but panics if an error occurs.
  456. func (lruo *LabelRelationshipUpdateOne) SaveX(ctx context.Context) *LabelRelationship {
  457. node, err := lruo.Save(ctx)
  458. if err != nil {
  459. panic(err)
  460. }
  461. return node
  462. }
  463. // Exec executes the query on the entity.
  464. func (lruo *LabelRelationshipUpdateOne) Exec(ctx context.Context) error {
  465. _, err := lruo.Save(ctx)
  466. return err
  467. }
  468. // ExecX is like Exec, but panics if an error occurs.
  469. func (lruo *LabelRelationshipUpdateOne) ExecX(ctx context.Context) {
  470. if err := lruo.Exec(ctx); err != nil {
  471. panic(err)
  472. }
  473. }
  474. // defaults sets the default values of the builder before save.
  475. func (lruo *LabelRelationshipUpdateOne) defaults() error {
  476. if _, ok := lruo.mutation.UpdatedAt(); !ok {
  477. if labelrelationship.UpdateDefaultUpdatedAt == nil {
  478. return fmt.Errorf("ent: uninitialized labelrelationship.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  479. }
  480. v := labelrelationship.UpdateDefaultUpdatedAt()
  481. lruo.mutation.SetUpdatedAt(v)
  482. }
  483. return nil
  484. }
  485. // check runs all checks and user-defined validators on the builder.
  486. func (lruo *LabelRelationshipUpdateOne) check() error {
  487. if _, ok := lruo.mutation.ContactsID(); lruo.mutation.ContactsCleared() && !ok {
  488. return errors.New(`ent: clearing a required unique edge "LabelRelationship.contacts"`)
  489. }
  490. if _, ok := lruo.mutation.LabelsID(); lruo.mutation.LabelsCleared() && !ok {
  491. return errors.New(`ent: clearing a required unique edge "LabelRelationship.labels"`)
  492. }
  493. return nil
  494. }
  495. func (lruo *LabelRelationshipUpdateOne) sqlSave(ctx context.Context) (_node *LabelRelationship, err error) {
  496. if err := lruo.check(); err != nil {
  497. return _node, err
  498. }
  499. _spec := sqlgraph.NewUpdateSpec(labelrelationship.Table, labelrelationship.Columns, sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64))
  500. id, ok := lruo.mutation.ID()
  501. if !ok {
  502. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "LabelRelationship.id" for update`)}
  503. }
  504. _spec.Node.ID.Value = id
  505. if fields := lruo.fields; len(fields) > 0 {
  506. _spec.Node.Columns = make([]string, 0, len(fields))
  507. _spec.Node.Columns = append(_spec.Node.Columns, labelrelationship.FieldID)
  508. for _, f := range fields {
  509. if !labelrelationship.ValidColumn(f) {
  510. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  511. }
  512. if f != labelrelationship.FieldID {
  513. _spec.Node.Columns = append(_spec.Node.Columns, f)
  514. }
  515. }
  516. }
  517. if ps := lruo.mutation.predicates; len(ps) > 0 {
  518. _spec.Predicate = func(selector *sql.Selector) {
  519. for i := range ps {
  520. ps[i](selector)
  521. }
  522. }
  523. }
  524. if value, ok := lruo.mutation.UpdatedAt(); ok {
  525. _spec.SetField(labelrelationship.FieldUpdatedAt, field.TypeTime, value)
  526. }
  527. if value, ok := lruo.mutation.Status(); ok {
  528. _spec.SetField(labelrelationship.FieldStatus, field.TypeUint8, value)
  529. }
  530. if value, ok := lruo.mutation.AddedStatus(); ok {
  531. _spec.AddField(labelrelationship.FieldStatus, field.TypeUint8, value)
  532. }
  533. if lruo.mutation.StatusCleared() {
  534. _spec.ClearField(labelrelationship.FieldStatus, field.TypeUint8)
  535. }
  536. if value, ok := lruo.mutation.DeletedAt(); ok {
  537. _spec.SetField(labelrelationship.FieldDeletedAt, field.TypeTime, value)
  538. }
  539. if lruo.mutation.DeletedAtCleared() {
  540. _spec.ClearField(labelrelationship.FieldDeletedAt, field.TypeTime)
  541. }
  542. if value, ok := lruo.mutation.OrganizationID(); ok {
  543. _spec.SetField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
  544. }
  545. if value, ok := lruo.mutation.AddedOrganizationID(); ok {
  546. _spec.AddField(labelrelationship.FieldOrganizationID, field.TypeUint64, value)
  547. }
  548. if lruo.mutation.OrganizationIDCleared() {
  549. _spec.ClearField(labelrelationship.FieldOrganizationID, field.TypeUint64)
  550. }
  551. if lruo.mutation.ContactsCleared() {
  552. edge := &sqlgraph.EdgeSpec{
  553. Rel: sqlgraph.M2O,
  554. Inverse: true,
  555. Table: labelrelationship.ContactsTable,
  556. Columns: []string{labelrelationship.ContactsColumn},
  557. Bidi: false,
  558. Target: &sqlgraph.EdgeTarget{
  559. IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
  560. },
  561. }
  562. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  563. }
  564. if nodes := lruo.mutation.ContactsIDs(); len(nodes) > 0 {
  565. edge := &sqlgraph.EdgeSpec{
  566. Rel: sqlgraph.M2O,
  567. Inverse: true,
  568. Table: labelrelationship.ContactsTable,
  569. Columns: []string{labelrelationship.ContactsColumn},
  570. Bidi: false,
  571. Target: &sqlgraph.EdgeTarget{
  572. IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
  573. },
  574. }
  575. for _, k := range nodes {
  576. edge.Target.Nodes = append(edge.Target.Nodes, k)
  577. }
  578. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  579. }
  580. if lruo.mutation.LabelsCleared() {
  581. edge := &sqlgraph.EdgeSpec{
  582. Rel: sqlgraph.M2O,
  583. Inverse: true,
  584. Table: labelrelationship.LabelsTable,
  585. Columns: []string{labelrelationship.LabelsColumn},
  586. Bidi: false,
  587. Target: &sqlgraph.EdgeTarget{
  588. IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
  589. },
  590. }
  591. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  592. }
  593. if nodes := lruo.mutation.LabelsIDs(); len(nodes) > 0 {
  594. edge := &sqlgraph.EdgeSpec{
  595. Rel: sqlgraph.M2O,
  596. Inverse: true,
  597. Table: labelrelationship.LabelsTable,
  598. Columns: []string{labelrelationship.LabelsColumn},
  599. Bidi: false,
  600. Target: &sqlgraph.EdgeTarget{
  601. IDSpec: sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64),
  602. },
  603. }
  604. for _, k := range nodes {
  605. edge.Target.Nodes = append(edge.Target.Nodes, k)
  606. }
  607. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  608. }
  609. _node = &LabelRelationship{config: lruo.config}
  610. _spec.Assign = _node.assignValues
  611. _spec.ScanValues = _node.scanValues
  612. if err = sqlgraph.UpdateNode(ctx, lruo.driver, _spec); err != nil {
  613. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  614. err = &NotFoundError{labelrelationship.Label}
  615. } else if sqlgraph.IsConstraintError(err) {
  616. err = &ConstraintError{msg: err.Error(), wrap: err}
  617. }
  618. return nil, err
  619. }
  620. lruo.mutation.done = true
  621. return _node, nil
  622. }