sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Composite type field reference is wrong in generated query

Open kayshav opened this issue 1 year ago • 0 comments

Version

1.27.0

What happened?

I have a simple 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).
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).
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) if err != nil { When I manually altered the generated code to func (q *Queries) GetTagFor(ctx context.Context, arg GetTagForParams) ([]FleetmanagerSchemaTag, error) { rows, err := q.db.Query(ctx, getTagFor, arg.Owner.ObjId, arg.TenantID) if err != nil { the query works correctly and returns proper results

Relevant log output

GetTagsFor fetch failed failed to encode args[0]: unable to encode common.ObjRef{ObjId:603334166352788488, ObjType:"Site"} into binary format for record (OID 2249): cannot find encode plan

Database schema

CREATE TYPE obj_ref AS  (
    obj_id nonnull_int8,
    obj_type nonnull_string
);

CREATE TABLE tag (
   create_date TIMESTAMP DEFAULT NOW() NOT NULL,
   modified_date TIMESTAMP,
   id int8 NOT NULL DEFAULT generate_tsid(),
   tenant_id int8,
     PRIMARY KEY (id),

   name VARCHAR(255) NOT NULL,
   value VARCHAR(255) NOT NULL,
   system boolean,
   owner obj_ref NOT NULL
);

SQL queries

-- name: GetTagFor :many
SELECT *
FROM fleetmanager_schema.tag
WHERE $1 = (owner).obj_id  AND tenant_id=$2;

Configuration

No response

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

kayshav avatar Aug 14 '24 00:08 kayshav