M
M
Microp2019-05-16 15:18:33
go
Microp, 2019-05-16 15:18:33

Update multiple models go pg bulk update time.Time?

The problem is that when updating one model, everything works, and when trying to update several models, an error occurs about the impossibility to update the date (time.Time go object
)

type Object struct {
  ID string `sql:",notnull"`
  StartAt time.Time `sql:",notnull"`
  Body string `sql:",notnull"`
}

I have a slice of these objects passed to the function
objects := []*Object
Update the time of the objects
for _, object := range objects {
  object.StartAt = time.Now()
}

then the
working code is executed
for _, object := range objects {
    _, err := s.db.Model(&objects).Column("start_at", "body").Update()
  
    if err != nil {
      return err
    }
  }

Non-working code
_, err := s.db.Model(&objects).Column("start_at", "body").Update()

I get error
77 -> column "start_at" is of type timestamp with time zone but expression is of type text
indicating that I'm trying to save text instead of time with time zone
. What is the problem is not entirely clear and how to solve it too. If someone came across I will be grateful for advice on a solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
terrier, 2019-05-17
@Microp

I tried to run your code with updating several models - everything seems to be working fine.
And log what SQL query go-pg generates and show us.
I managed

UPDATE "objects" AS "object" SET "start_at" = _data."start_at", "body" = _data."body"
                FROM (VALUES ('2019-05-16 21:14:19.579398+00:00:00'::timestamptz, 'body', '1'),
                          ('2019-05-16 21:14:19.579398+00:00:00'::timestamptz, 'body', '2')) 
                                  _data("start_at", "body", "id") WHERE "object"."id" = _data."id"

and everything passes.
go-pg, by the way, a fresh version?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question