Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column override with query alias produces wrong type #1752

Closed
ghost opened this issue Jul 21, 2022 · 1 comment · Fixed by #1884
Closed

Column override with query alias produces wrong type #1752

ghost opened this issue Jul 21, 2022 · 1 comment · Fixed by #1884

Comments

@ghost
Copy link

ghost commented Jul 21, 2022

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

@ghost ghost added bug Something isn't working triage New issues that hasn't been reviewed labels Jul 21, 2022
@ghost ghost changed the title Column override with query alias malfunction Column override with query alias produces wrong type Jul 21, 2022
@voluntas
Copy link

@WesleyMiller1998 #1208 looks like this same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants