Skip to content

Does not generate correct struct field name when using COALESCE #1213

Closed
@marcustut

Description

@marcustut

Version

1.10.0

What happened?

Suppose when I do the 'AS' to rename column in SQL, the generated struct field should be that name.

Note: I am running on the latest commit 9513155

I tried constructing another query with coalesce on the playground, it generates the field name correctly. So I suspect is something on the latest commit.

Note that in the following GetProductRow struct, the Coalesce field actually should be named Stock

Relevant log output

type GetProductRow struct {
	ProductID   uuid.UUID             `json:"product_id"`
	Name        string                `json:"name"`
	Active      bool                  `json:"active"`
	Description dbtype.JsonNullString `json:"description"`
	Metadata    pgtype.JSONB          `json:"metadata"`
	Images      []string              `json:"images"`
	CreatedAt   dbtype.JsonNullTime   `json:"created_at"`
	UpdatedAt   dbtype.JsonNullTime   `json:"updated_at"`
	Coalesce    interface{}           `json:"coalesce"`
	Price       pgtype.JSONB          `json:"price"`
	Brand       pgtype.JSONB          `json:"brand"`
}

Database schema

CREATE TABLE public.products (
    product_id uuid NOT NULL,
    name character varying(100) NOT NULL,
    active boolean NOT NULL,
    description text,
    metadata jsonb,
    brand_id uuid,
    images text[],
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now(),
    default_price_id uuid
);


CREATE TABLE public.prices (
    price_id uuid NOT NULL,
    product_id uuid NOT NULL,
    active boolean NOT NULL,
    unit_amount double precision NOT NULL,
    currency character(3) NOT NULL,
    metadata jsonb,
    brand_id uuid,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);

CREATE TABLE public.brands (
    brand_id uuid NOT NULL,
    rating double precision DEFAULT 0 NOT NULL,
    name character varying(255) NOT NULL,
    address jsonb NOT NULL,
    profile_image text,
    cover_image text,
    created_at timestamp without time zone DEFAULT now() NOT NULL,
    updated_at timestamp without time zone DEFAULT now() NOT NULL,
    email text,
    phone text,
    website text,
    stripe_account_id text
);

SQL queries

-- name: GetProduct :one
SELECT P.product_id,
       P.name,
       P.active,
       P.description,
       P.metadata,
       P.images,
       P.created_at,
       P.updated_at,
       COALESCE(SUM(S.quantity), 0) AS "stock",
       JSONB_BUILD_OBJECT(
               'price_id', PR.price_id,
               'unit_amount', PR.unit_amount,
               'currency', PR.currency,
               'metadata', PR.metadata
           )
                                    AS "price",
       JSONB_BUILD_OBJECT(
               'brand_id', B.brand_id,
               'rating', B.rating,
               'name', B.name,
               'profile_image', B.profile_image,
               'phone', B.phone,
               'email', B.email,
               'website', B.website
           )                        AS "brand"
FROM public.products P
         LEFT JOIN public.prices PR ON PR.product_id = P.product_id
         LEFT JOIN public.brands B ON B.brand_id = P.brand_id
         LEFT JOIN public.stock S on S.product_id = P.product_id
WHERE P.product_id = $1
  AND P.default_price_id = PR.price_id
  AND PR.active = true
GROUP BY P.product_id, PR.price_id, B.brand_id
LIMIT 1;

Configuration

version: '1'
packages:
  - name: 'db'
    path: 'db'
    queries: './db/query/'
    schema: './db/schema.sql'
    engine: 'postgresql'
    sql_package: 'pgx/v4'
    emit_json_tags: true
    emit_prepared_queries: true
    emit_interface: false
    emit_exact_table_names: true
    emit_empty_slices: false
    json_tags_case_style: 'snake'
    overrides:
      # Datatype overrides
      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullString'
        db_type: 'text'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullString'
        db_type: 'pg_catalog.varchar'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullString'
        db_type: 'pg_catalog.bpchar'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullString'
        db_type: 'string'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullTime'
        db_type: 'date'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullTime'
        db_type: 'timestamptz'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullTime'
        db_type: 'pg_catalog.timestamptz'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullTime'
        db_type: 'pg_catalog.timestamp'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullBool'
        db_type: 'boolean'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullBool'
        db_type: 'bool'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullBool'
        db_type: 'pg_catalog.bool'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullInt32'
        db_type: 'integer'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullInt32'
        db_type: 'int'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullInt32'
        db_type: 'int4'
        nullable: true

      - go_type: 'github.com/CTPL-Yabee/yabee/gobackend/pkg/dbtype.JsonNullInt32'
        db_type: 'pg_catalog.int4'
        nullable: true

      # Per-column overrides

Playground URL

https://play.sqlc.dev/p/dfba09f5f00a1a12625473f33782157047d713b8a3acd64695ee32c3bcd69ea7

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions