I have a simple postgres sql query with a WHERE clause that references a field of a composite type.
const getTagFor = -- name: GetTagFor :many
SELECT create_date, modified_date, id, tenant_id, name, value, system, owner FROM fleetmanager_schema.tag WHERE $1 = (owner).obj_id AND tenant_id=$2
In the query above, owner is a composite type obj_ref (Go: ObjRef). The intent is to match the owner.obj_id field of the compsite with the supplird arg $1.
type ObjRef struct {
ObjId int64 json:"obj_id" range:"min=8,max=32"
ObjType string json:"obj_type" range:"min=3,max=32"
}
The generated query from sqlc seems to be distorting the query. It passes the entire arg.Owner object leading to a failure in pgx (unable to encode type). Here is the generated code.
type GetTagForParams struct {
Owner common.ObjRef
TenantID pgtype.Int8
}
func (q *Queries) GetTagFor(ctx context.Context, arg GetTagForParams) ([]FleetmanagerSchemaTag, error) {
rows, err := q.db.Query(ctx, getTagFor, arg.Owner, arg.TenantID)
This query fails with db error because it can’t compare a composite type to an int64.
When I manually altered the generated code to
rows, err := q.db.Query(ctx, getTagFor, arg.Owner.ObjId, arg.TenantID)
if err != nil {
the query works correctly and returns proper results.
Does anyone know if there’s some flag or attribute I need to provide to sqlc to generate the correct code?