我使用https://github.com/kyleconroy/sqlc
这是一个用于生成代码的库。我的查询
-- name: SetAssignmentsResult :exec
UPDATE assignments
SET status = 'ACCEPTED',
result = $1
WHERE task_id = $2
AND item_id = $3
AND marker_id = $4;
sqlc 生成此代码
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
}
我的代码
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,
},
}
返回错误:ERROR: invalid input syntax for type json (SQLSTATE 22P02)
重要的。如果您使用空 json,那么一切正常
err := repo.SetResult(ctx,
godb.SetAssignmentsResultParams{
pqtype.NullRawMessage{},
IDs[0],
IDs[1],
uuid.NullUUID{IDs[2], true},
},