3

I use Go + Postgres. To work with Postgres I use pgx. I have an array of UUID in my Postgres table and a Struct with []*uuid.UUID (from github.com/gofrs/uuid) in Go (see code below).

The problem is that I always get error when I try to use AssignTo function

cannot: decode &pgtype.UUIDArray{Elements:[]pgtype.UUID(nil), Dimensions:[]pgtype.ArrayDimension(nil), Status:0x2} into *[]*uuid.UUID

(I have tried to use all combinations with pointers and it doesn't work)

My struct (for API):

type Book struct {
    AuthorIds []*uuid.UUID `json:"author_ids"`
}

My struct (for working with Postgres):

type PgBook struct {
    AuthorIds pgtype.UUIDArray `db:"author_ids"`
}

And I use AssignTo function from pgx

err := pgBook.AuthorIds.AssignTo(&book.AuthorIds)

How to solve this problem?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Klimbo
  • 1,308
  • 3
  • 15
  • 34
  • 1
    And `uuid.UUID` is from ... ? – mkopriva Oct 22 '19 at 11:45
  • 2
    From gofrs package https://github.com/gofrs/uuid (i wrote it in the title) – Klimbo Oct 22 '19 at 11:49
  • 1
    You might be able to do it by changing the `Book`'s field to `AuthorIds []uuid.UUID` (i.e. slice of non-pointers). If not you'll have to first assign to something that pgx understands and then manually set the `Book`'s field. – mkopriva Oct 22 '19 at 11:50
  • 1
    ... i.e. declare a variable of type `[][16]byte`, pass a pointer to it to `AssignTo`, after that returns loop over that variable and convert each element into a `uuid.UUID` and append that to the destination slice. – mkopriva Oct 22 '19 at 11:58
  • 1
    I have tried []uuid.UUID bit it does not work too. But the strange part is that AssignTo works well with pgtype.UUID (p.s. I have uuid field in db), but not pgtype.UUIDArray – Klimbo Oct 22 '19 at 11:59
  • 1
    I have thought that I did mistake. If not (it is pgx error) I will do it manually – Klimbo Oct 22 '19 at 12:01

0 Answers0