jackc / jackc/pgx

[Question] How can I Insert an array of a custom postgres type using driver.Value

Open
#1,089 3 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

Hello,
I am trying to get the following to work:
```go
type StickerState struct {
ID int
Wear int
Slot int
}

type StickerStates []StickerState

func (s StickerStates) Value() (driver.Value, error) {
if len(s) == 0 {
return "{}", nil
}
var rows []string
for _, entry := range s {
row := fmt.Sprintf("(%d, %d, %d)", entry.ID, entry.Wear, entry.Slot)
rows = append(rows, row)
}
return fmt.Sprintf("(%s)", strings.Join(rows, ",")), nil
}

type MarketListing struct {
...
Stickers StickerStates
...
}
```

When passing my go slice of type StickerState i get the following error while inserting:
`ERROR: malformed array literal: "((0, 100, 0))" (SQLSTATE 22P02)`

Contributor guide

Open the contributing guide

Research direction

Start with the StickerStates.Value method and the failing insert described in the issue; review how pgx handles driver.Valuer values for PostgreSQL custom types and arrays. Reproduce the minimal insertion and verify that the resulting value is accepted without the malformed array literal error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.