sqlc-dev / sqlc

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

Failed to generate when schema datetime is not null #3643

Open afikrim opened 1 month ago

afikrim commented 1 month ago

Version

1.27.0

What happened?

I want to generate sql query using copyfrom annotations and got an error

Relevant log output

# package bankstatement
error generating code: values with a timezone are not yet supported

Database schema

CREATE TABLE `bank_statements` (
  `id` int NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=17136 DEFAULT CHARSET=latin1

SQL queries

-- name: CreateBankStatements :copyfrom
INSERT INTO bank_statements (created_at, updated_at)
  VALUES (
    sqlc.arg(created_at),
    sqlc.arg(updated_at)
  );

Configuration

version: "2"
overrides:
  go:
    overrides:
      - db_type: int
        go_type: int64
      - db_type: int
        nullable: true
        go_type:
          import: github.com/guregu/null/v5
          type: Int64
      - db_type: tinyint
        go_type: int64
      - db_type: tinyint
        nullable: true
        go_type:
          import: github.com/guregu/null/v5
          type: Int64
      - db_type: varchar
        nullable: true
        go_type:
          import: github.com/guregu/null/v5
          type: String
      - db_type: datetime
        nullable: true
        go_type:
          import: github.com/guregu/null/v5
          type: Time
      - db_type: decimal
        nullable: true
        go_type:
          import: github.com/guregu/null/v5
          type: String
      - db_type: date
        nullable: true
        go_type:
          import: github.com/guregu/null/v5
          type: String
sql:
  - name: bank_statement_sql_gen
    engine: mysql
    queries:
      - ./db/query/create_bank_statements.sql
    schema:
      - ./db/schema/bank_statements.sql
    gen:
      go:
        package: bankstatement
        sql_package: database/sql
        sql_driver: github.com/go-sql-driver/mysql
        out: pkg/repository/gen/bank_statement
        emit_db_tags: true
        emit_result_struct_pointers: true
        emit_params_struct_pointers: true
        emit_methods_with_db_argument: true
        emit_interface: true
        omit_unused_structs: true

Playground URL

https://play.sqlc.dev/p/7efe51f305a19f7d9c847f5a4bcb6f9004d64ea5f3383b51fe081a4ade0fd72f

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

bignyap commented 1 month ago

I also encountered the same issue. The issue is coming from https://github.com/sqlc-dev/sqlc/blob/8bf28178dd494a0cde554995e21bba0ed10d523b/internal/codegen/golang/gen.go#L351

  1. What's the reason to add the above constraint?
  2. Is there any workaround?
afikrim commented 3 weeks ago

I also encountered the same issue. The issue is coming from

https://github.com/sqlc-dev/sqlc/blob/8bf28178dd494a0cde554995e21bba0ed10d523b/internal/codegen/golang/gen.go#L351

  1. What's the reason to add the above constraint?
  2. Is there any workaround?

For now, I make the schema nullable, resulting in successful code generation. This could be the workaround if you're defining the schema instead of using db migration as the code generation source of truth