sqlc-dev / sqlc

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

sql type Time being converted to Golang time.Time, what could not work #3650

Open Skopjuk opened 1 month ago

Skopjuk commented 1 month ago

Version

1.25.0

What happened?

I have a table with a column typed TIME NOT NULL. I generated code for select statement from this table and received an error

"sql: Scan error on column index 2, name "time": unsupported Scan, storing driver.Value type []uint8 into type *time.Time".

parseTime = true, already, that's not a point. As far I figured out at the moment the problem that sqlc generated struct for this table and for the column typed TIME used Golang time.Time type. But Golang can not convert to time.Time the structure without a date, so that's the problem. Maybe it would be betted to convert TIME to time.Time directly.

code sqlc generated:

type VideoTiming struct {
 ID          string
 VideoID     string
 Time        time.Time
 Description sql.NullString
 CreatedAt   time.Time
 DeletedAt   sql.NullTime
}

func (q *Queries) GetVideoTimings(ctx context.Context, videoID string) ([]VideoTiming, error) {
 rows, err := q.db.QueryContext(ctx, getVideoTimings, videoID)
 if err != nil {
  return nil, err
 }
 defer rows.Close()
 var items []VideoTiming
 for rows.Next() {
  var i VideoTiming
  if err := rows.Scan(
   &i.ID,
   &i.VideoID,
   &i.Time,
   &i.Description,
   &i.CreatedAt,
   &i.DeletedAt,
  ); err != nil {
   return nil, err
  }
  items = append(items, i)
 }
 if err := rows.Close(); err != nil {
  return nil, err
 }
 if err := rows.Err(); err != nil {
  return nil, err
 }
 return items, nil
}

Relevant log output

No response

Database schema

CREATE TABLE IF NOT EXISTS timings (
    id VARCHAR(36) PRIMARY KEY,
    video_id VARCHAR(36) NOT NULL,
    timeMark INT NOT NULL,
    description VARCHAR(500),
    created_at       TIMESTAMP NOT NULL,
    deleted_at       TIMESTAMP
);

SQL queries

-- name: GetVideoTimings :many
SELECT * FROM video_timings WHERE video_id = ? AND deleted_at IS NULL;

Configuration

version: "2"
cloud:
    organization: ""
    project: ""
    hostname: ""
sql:
    - engine: "mysql"
      queries: "/queries/"
      schema: "/migrations/"
      gen:
          go:
              package: "repository"
              out: "repository"
overrides:
    go: null
plugins: []
rules: []
options: {}

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go