1 year ago
#235843
Nakem1
inserting json to db
I use https://github.com/kyleconroy/sqlc
This is a library for generating code. My query
-- name: SetAssignmentsResult :exec
UPDATE assignments
SET status = 'ACCEPTED',
result = $1
WHERE task_id = $2
AND item_id = $3
AND marker_id = $4;
sqlc generate this code
type SetAssignmentsResultParams struct {
Result pqtype.NullRawMessage `json:"result"`
TaskID uuid.UUID `json:"task_id"`
ItemID uuid.UUID `json:"item_id"`
MarkerID uuid.NullUUID `json:"marker_id"`
}
func (q *Queries) SetAssignmentsResult(ctx context.Context, arg SetAssignmentsResultParams) error {
_, err := q.exec(ctx, q.setAssignmentsResultStmt, setAssignmentsResult,
arg.Result,
arg.TaskID,
arg.ItemID,
arg.MarkerID,
)
return err
}
My code
err := repo.SetResult(ctx,
godb.SetAssignmentsResultParams{
Result: pqtype.NullRawMessage{RawMessage: []byte(`{"test": "0.0.0.0:8080"}`),
Valid: true,
},
TaskID: IDs[0],
ItemID: IDs[1],
MarkerID: uuid.NullUUID{
UUID: IDs[2],
Valid: true,
},
}
Return error: ERROR: invalid input syntax for type json (SQLSTATE 22P02)
Important. If you use an empty json, then everything works
err := repo.SetResult(ctx,
godb.SetAssignmentsResultParams{
pqtype.NullRawMessage{},
IDs[0],
IDs[1],
uuid.NullUUID{IDs[2], true},
},
sql
json
database
go
sqlc
0 Answers
Your Answer