- 
                Notifications
    
You must be signed in to change notification settings  - Fork 253
 
Description
Describe the bug
When using sponge to auto-generate code from a database with timestamptz (timestamp with time zone) columns, the tool incorrectly maps these columns to Go's string type instead of time.Time. This causes SQL insertion errors because empty strings (default for unassigned string fields) cannot be parsed as valid timestamptz values by the database.
使用 sponge 从包含timestamptz(带时区的时间戳)字段的数据库自动生成代码时,工具会错误地将这些字段映射为 Go 语言的string类型(而非time.Time)。这会导致 SQL 插入错误,因为未赋值的字符串字段默认值为空字符串"",而数据库无法将空字符串解析为有效的timestamptz值。
To Reproduce
Steps to reproduce the behavior:
The database table structure (simplified):
CREATE TABLE tags (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  created_at TIMESTAMPTZ,  -- timestamptz type
);Use sponge to auto-generate model code for this table. The generated Go struct is:
type Tag struct {
    ID        int    `gorm:"column:id;primaryKey" json:"id"`
    Name      string `gorm:"column:name" json:"name"`
    CreatedAt string `gorm:"column:created_at;type:timestamptz" json:"createdAt"`  // Incorrectly mapped to string
}Call the generated create API (e.g., POST /api/v1/tags/) with payload {"name": "tag1"} (no createdAt/updatedAt provided).
The error occurs:
plaintext
ERROR: invalid input syntax for type timestamp with time zone: "" (SQLSTATE 22007)
SQL: INSERT INTO "tags" ("name","created_at") VALUES ('tag1','') RETURNING "id"
--- 中文 ---
复现步骤:
数据库表结构(简化版):
CREATE TABLE tags (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  created_at TIMESTAMPTZ,  -- 类型为timestamptz
);使用 sponge 为该表自动生成模型代码,生成的 Go 结构体如下:
type Tag struct {
    ID        int    `gorm:"column:id;primaryKey" json:"id"`
    Name      string `gorm:"column:name" json:"name"`
    CreatedAt string `gorm:"column:created_at;type:timestamptz" json:"createdAt"`  // 错误映射为string
}调用生成的创建接口(例如POST /api/v1/tags/),请求体为{"name": "标签1"}(未提供createdAt/updatedAt字段)。
出现错误:
plaintext
ERROR: invalid input syntax for type timestamp with time zone: "" (SQLSTATE 22007)
SQL: INSERT INTO "tags" ("name","created_at") VALUES ('标签1','') RETURNING "id"
Environments (please complete the following information):
- OS: Windows
 - sponge version https://go-sponge.com/ui/web-http