Skip to content

SELECT foo.id AS foo_id loses Go type information #606

Description

@tv42

I'm trying to use custom Go types for some SQL columns, mapping a sql int64 to Go type FooID uint64.

It seems I can have

SELECT foo.id FROM ... and get generated type FooRow struct { ID FooID }

or I can have

SELECT foo.id AS foo_id FROM ... and get type FooRow struct { FooID int64 }

but I cannot get both at the same time. Using the AS forgets the association with the Go type, but without the AS my struct fields are kinda misleading. Consider SELECT foo.id, bar.id FROM ..., can't have two things fighting for the struct field name ID.

Activity

  1. kyleconroy commented on Aug 28, 2021

    @kyleconroy
    Collaborator

    @tv42 Can you provide a complete example for this issue? Since this involves type overrides, can you include a sqlc configuration file, database schema, and example queries.

  2. tv42 commented on Aug 31, 2021

    @tv42
    Author

    Here's a full repro with the latest versions: https://gist.github.com/tv42/34d87e0975739f20654eac4894380511

    To summarize:

    -- name: BadType :one
    SELECT users.id AS user_id, 42 as filler FROM users LIMIT 1;
    
    -- name: BadName :one
    SELECT users.id, 42 as filler FROM users LIMIT 1;
    
        overrides:
          - column: users.id
            go_type: example.com/sqlc_issues606_reproducer.UserID
    

    How it goes wrong:

    $ go generate
    $ grep -A 1 'type Bad' query.sql.go
    type BadNameRow struct {
    	ID     sqlc_issues606_reproducer.UserID
    --
    type BadTypeRow struct {
    	UserID int64
    
  3. tv42 commented on Sep 8, 2021

    @tv42
    Author

    Can you please re-open this? It's still there, as the previous comment shows.

  4. kyleconroy commented on Sep 8, 2021

    @kyleconroy
    Collaborator

    @tv42 whoops, sorry about that. Thanks for writing up the full example.

  5. alarbada commented on Apr 5, 2022

    @alarbada

    I've personally tested this one with sqlc v1.13.0 and with type overrides this can be easily worked around. Could that solve it @tv42 ?

  6. tv42 commented on Apr 19, 2022

    @tv42
    Author

    I've personally tested this one with sqlc v1.13.0 and with type overrides this can be easily worked around. Could that solve it @tv42 ?

    Sorry that's too vague to say much. The reproducing example already uses type overrides to switch from an int64 to UserID, do you think it is somehow wrong?

        overrides:
          - column: users.id
            go_type: example.com/sqlc_issues606_reproducer.UserID
    

    The bug seems to be that using SELECT column_name AS alias makes the type overrides not be applied.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions