Skip to content

Type override not being applied #412

Closed

Description

@kyleconroy

A user wrote an email to me with the following issue:

Hi again, I have column

battery             INTEGER,

and the result is

Battery           sql.NullInt32

I cannot force to use sql.NullInt64

overrides:
  - go_type: "int64"
    db_type: "integer"

is this bug or we use this in wrong way?

Activity

  1. kyleconroy commented on Mar 20, 2020

    @kyleconroy
    CollaboratorAuthor

    A few things: I wouldn't force the integer PostgreSQL type to an int64, as it's a int32 internally. You should be using bigint for that

    If you still want to override the type, you need to do this:

    overrides:
      - go_type: "sql.NullInt64"
        db_type: "integer"
        null: true
    

    You need a separate override for null and non-null values.

  2. lsjurczak commented on Mar 28, 2020

    @lsjurczak

    Hi @kyleconroy. Let's continue this issue here.

    I have following schema:

    CREATE TABLE example
    (
        battery INTEGER
    );

    and query:

    -- name: CreateExample :one
    INSERT INTO example (
        battery
    )
    VALUES (
        $1
    )
    RETURNING *;

    Config that I'm using

    overrides:
      - go_type: "database/sql.NullInt64"
        db_type: "integer"
        null: true  // I tried also "null" as mentioned in #413
    

    The result is:

    type Example struct {
    	Battery sql.NullInt32
    }
    
    func (q *Queries) CreateExample(ctx context.Context, battery sql.NullInt32) (sql.NullInt32, error) {
    	row := q.db.QueryRowContext(ctx, createExample, battery)
    	var battery sql.NullInt32
    	err := row.Scan(&battery)
    	return battery, err
    }
  3. kyleconroy commented on Mar 28, 2020

    @kyleconroy
    CollaboratorAuthor

    @lsjurczak Sorry for not getting to the root of the issue

    I've reproduced the bug. To get the desired output, the overrides section in your sqlc.yaml file needs to look like this:

    overrides:
      - go_type: "database/sql.NullInt64"
        db_type: "pg_catalog.int4"
        "null": true

    I understand this behavior is confusing and unexpected. The SQL parser we use for Postgres automatically converts type names into a canonical format. In this case, it translates integer to pg_catalog.int4. I'll open up a separate issue to fix this.

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

    📚 postgresqlbugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions