Skip to content

Column override with query alias produces wrong type #1752

Closed
@ghost

Description

Version

1.14.0

What happened?

This bug has been reported before in #606, but I wanted to bump the issue and provide a work around!

The issue is that with the setup provided, and similar setups, there is an issue with query aliases and type overrides. I'm trying to override the assets.total field, but when aliasing it in the query we can the following struct output.

type GetAssetRow struct {
	AssetID       int64
	AssetTotal    interface{}
	AssetDecimals int32
	AssetName     sql.NullString
	AssetUnitName sql.NullString
	Creator       string
}

I went through and output the plugin.Column values while returning the goType in the log output. As you can see, the name for the types in "columns" in the queries is now set as the alias. This means that when checking for an override, we can't find one. I thought for a while about how to solve this problem without changing the architecture, but I think an extra field may need to be added to the plugin.Column struct.

Meanwhile, we can actually work around this problem pretty simply by adding another override to our sqlc.yaml. All I have to do is make my overrides read

overrides:
          - column: "assets.total"
            go_type: "github.com/CavernaTechnologies/pgext.Puint"
          - column: "assets.asset_total"
            go_type: "github.com/CavernaTechnologies/pgext.Puint"

This is certainly not elegant or ideal, but it does work. Considering how niche this particular issue appears to be, I don't know if a fix is desperately needed either. I hope this helps anyone running into a similar issue!

Relevant log output

name:"asset_id"  not_null:true  length:-1  table:{name:"assets"}  type:{schema:"pg_catalog"  name:"int8"}
name:"asset_total"  not_null:true  length:-1  table:{name:"assets"}  type:{name:"uint64"}
name:"asset_decimals"  not_null:true  length:-1  table:{name:"assets"}  type:{schema:"pg_catalog"  name:"int4"}
name:"asset_name"  length:-1  table:{name:"assets"}  type:{schema:"pg_catalog"  name:"varchar"}
name:"asset_unit_name"  length:-1  table:{name:"assets"}  type:{schema:"pg_catalog"  name:"varchar"}
name:"creator"  not_null:true  length:-1  table:{name:"assets"}  type:{schema:"pg_catalog"  name:"varchar"}

Database schema

CREATE DOMAIN uint64 AS numeric(20,0) CHECK(0 <= VALUE AND VALUE <= 18446744073709551615);

CREATE TABLE
    assets (
        id              bigint          PRIMARY KEY CHECK(id >= 0),
        total           uint64          NOT NULL,
        decimals        int             NOT NULL CHECK(decimals >= 0 AND decimals <= 19),
        asset_name      VARCHAR(32),
        unit_name       VARCHAR(8),
        creator_address VARCHAR(58)     NOT NULL CHECK(length(creator_address) = 58)
    );

SQL queries

-- name: GetAsset :one
SELECT
    id AS asset_id,
    total AS asset_total,
    decimals AS asset_decimals,
    asset_name AS asset_name,
    unit_name AS asset_unit_name,
    creator_address AS creator
FROM assets WHERE id = $1 LIMIT 1;

Configuration

version: 2
sql:
  - schema: "table.sql"
    queries: "query.sql"
    engine: "postgresql"
    gen:
      go:
        package: "database"
        out: "database"
        sql_package: "pgx/v4"
        overrides:
          - column: "assets.total"
            go_type: "github.com/CavernaTechnologies/pgext.Puint"

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions