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

[MySQL] TIMESTAMP NULL DEFAULT NULL 类型无法使用 gorm tag 创建 #7314

Open
mmggzz opened this issue Dec 11, 2024 · 0 comments
Open

[MySQL] TIMESTAMP NULL DEFAULT NULL 类型无法使用 gorm tag 创建 #7314

mmggzz opened this issue Dec 11, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@mmggzz
Copy link

mmggzz commented Dec 11, 2024

Your Question

目标表结构

CREATE TABLE `test` (
    `id` INT ( 11 ) AUTO_INCREMENT NOT NULL,
    `name` VARCHAR ( 20 ) NULL DEFAULT NULL,
    `resp_time` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY ( `id` ))

golang 结构体

type Test struct {
	ID       int        `gorm:"autoIncrement:true;primaryKey;column:id;type:int(11);not null" json:"id"`
	Name     string     `gorm:"column:name;type:varchar(20);not null;default:''" json:"name"`
	RespTime *time.Time `gorm:"column:resp_time;type:timestamp;default:null" json:"resp_time"`
	//RespTime *time.Time `gorm:"column:resp_time;type:timestamp" json:"resp_time"`
	//RespTime time.Time  `gorm:"column:resp_time;type:timestamp;default:null" json:"resp_time"`
}

gorm 生成的表结构创建语句:

CREATE TABLE `test` (
    `id` INT ( 11 ) AUTO_INCREMENT NOT NULL,
    `name` VARCHAR ( 20 ) NULL DEFAULT NULL,
    `resp_time` TIMESTAMP DEFAULT NULL,
PRIMARY KEY ( `id` ))

执行报错:

Error 1067 (42000): Invalid default value for 'resp_time'

Expected answer

期望的 SQL 为: resp_time TIMESTAMP NULL DEFAULT NULL
gorm 生成的 SQL 为:resp_time TIMESTAMP DEFAULT NULL

Issue#7127

@mmggzz mmggzz added the type:question general questions label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants