sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.3k stars 804 forks source link

MySQL column does not exist #1368

Closed lmtyler closed 1 year ago

lmtyler commented 2 years ago

Version v1.11.0 - brew v1.7.1-devel - built from code clone

OS MocOS

What happened? sqlc generate fails to find a column

Relevant log output

# package db
sql/channel_approval_request_type.sql:14:1: column "request_type_id" does not exist
sql/channel_approval_request_type.sql:19:1: column "request_type_id" does not exist
sql/channel_approval_request_type.sql:23:1: column "request_type_id" does not exist

SQL FILE

CREATE TABLE IF NOT EXISTS channel_approval_request_type
(
    request_type_id smallint(5) unsigned NOT NULL,
    PRIMARY KEY (request_type_id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

/* name: ListChannelApprovalRequestTypes :many */
select *
from channel_approval_request_type
order by request_type_id;

/* name: GetChannelApprovalRequestType :one */
select *
from channel_approval_request_type
where request_type_id = ?;

/* name: AddChannelApprovalRequestType :exec */
insert into channel_approval_request_type (request_type_id)
values (?);

/* name: DeleteChannelApprovalRequestType :exec */
delete
from channel_approval_request_type
where request_type_id = ?;

Configuration

{
  "version": "1",
  "packages": [
    {
      "name": "db",
      "path": "./",
      "schema": "./sql/",
      "queries": "./sql/",
      "engine": "mysql",
      "emit_exact_table_names": false,
      "emit_empty_slices": false,
      "emit_json_tags": true,
      "emit_interface": true,
      "emit_prepared_queries": true
    }
  ]
}
lmtyler commented 2 years ago

I changed SQL file to

CREATE TABLE IF NOT EXISTS channel_approval_request_type
(
    request_type_id SMALLINT(5) UNSIGNED NOT NULL,
    PRIMARY KEY (request_type_id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

/* name: ListChannelApprovalRequestTypes :many */
select *
from channel_approval_request_type
order by request_type_id;

This is what I got as output which is still incorrect, but it seems to work from playground

// Code generated by sqlc. DO NOT EDIT.
// source: channel_approval_request_type.sql

package db

import (
    "context"
)

const listChannelApprovalRequestTypes = `-- name: ListChannelApprovalRequestTypes :many
select channel_approval_request_type
from channel_approval_request_type
order by request_type_id
`

func (q *Queries) ListChannelApprovalRequestTypes(ctx context.Context) ([]int32, error) {
    rows, err := q.query(ctx, q.listChannelApprovalRequestTypesStmt, listChannelApprovalRequestTypes)
    if err != nil {
        return nil, err
    }
    defer rows.Close()
    var items []int32
    for rows.Next() {
        var channel_approval_request_type int32
        if err := rows.Scan(&channel_approval_request_type); err != nil {
            return nil, err
        }
        items = append(items, channel_approval_request_type)
    }
    if err := rows.Close(); err != nil {
        return nil, err
    }
    if err := rows.Err(); err != nil {
        return nil, err
    }
    return items, nil
}

and in model.go

type ChannelApprovalRequestType struct {
    ChannelApprovalRequestType int32 `json:"channel_approval_request_type"`
}
kyleconroy commented 1 year ago

I checked if this is working in 1.21 and didn't get any errors

https://play.sqlc.dev/p/63d0e4263d6388089eedacd3f17f56e16f4ec5b65c08dac51bda41a3fcc244bb