Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time zone will be lost when using db.Exec() to create columns containing time #146

Open
xray-bit opened this issue Oct 23, 2024 · 0 comments

Comments

@xray-bit
Copy link

xray-bit commented Oct 23, 2024

When using db.Create() to create a column containing the time, it will contain the time zone

type Example struct {
	Id        int `gorm:"primaryKey;autoIncrement;"`
	CreatedAt time.Time
	UpdatedAt time.Time
	Name      string
}

func main() {
	db, err := gorm.Open(sqlite.Open("test.db"))
	if err != nil {
		panic(err)
	}

	db.AutoMigrate(new(Example))

	example := Example{Name: "Tome"}

	db.Debug().Create(&example)
}
[37.259ms] [rows:1] INSERT INTO `examples` (`created_at`,`updated_at`,`name`) VALUES ("2024-10-23 10:53:07.42","2024-10-23 10:53:07.42","Tome") RETURNING `id`

image

using db.Exec()

type Example struct {
	Id        int `gorm:"primaryKey;autoIncrement;"`
	CreatedAt time.Time
	UpdatedAt time.Time
	Name      string
}

func main() {
	db, err := gorm.Open(sqlite.Open("test.db"))
	if err != nil {
		panic(err)
	}

	db.AutoMigrate(new(Example))

	now := time.Now()
	example := Example{Name: "Tome", Id: 1, CreatedAt: now, UpdatedAt: now}

	sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
		return tx.Create(&example)
	})

	fmt.Println("sql:", sql)

	db.Exec(sql)
}
sql: INSERT INTO `examples` (`created_at`,`updated_at`,`name`,`id`) VALUES ("2024-10-23 10:58:19.003","2024-10-23 10:58:19.003","Tome",1) RETURNING `id`

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant