Closed xiyichan closed 2 years ago
看来似乎是指定列名的时候得使用大写
我也遇到跟你同样的问题。这样的话,代码里的字段名不就全部都大写吗。 还有其他解决方案吗?
我也遇到跟你同样的问题。这样的话,代码里的字段名不就全部都大写吗。 还有其他解决方案吗?
可以不指定列名就没问题了。
我也遇到跟你同样的问题。这样的话,代码里的字段名不就全部都大写吗。 还有其他解决方案吗?
可以不指定列名就没问题了。
type Setting struct {
ID uint gorm:"primary_key"
ServerAddress string json:"server_address"
ServerPort int json:"server_port"
Intranet int json:"intranet"
}
var settings []Setting
err := db.Find(&settings).Error
if err != nil {
log.Fatal(err)
return
} else {
fmt.Println(settings)
}
&{1 0 0}
我没有指定列名,代码这样子,返回值都是默认值。
请问你怎么解决的?
你有指定表名吗
你有指定表名吗
有的。
func (*Setting) TableName()string { return "settings" }
你这个主键命名好像不对
你这个主键命名好像不对
只有主键被映射出来了。其他的没有映射出来。
你这个主键命名好像不对
只有主键被映射出来了。其他的没有映射出来。
你把表删掉,自动建表试试呗,应该是没问题的
你这个主键命名好像不对
只有主键被映射出来了。其他的没有映射出来。
你把表删掉,自动建表试试呗,应该是没问题的
我是跟你的例子基本一致的。结果也跟你是一样的。 你是用什么其他方法呢
没有用什么其他方法呀。
type Project struct { ID int `json:"id" gorm:"column:id;primaryKey;autoIncrement"` Status int `json:"status" gorm:"column:status"` Product string `json:"product" gorm:"column:product"` Name string `json:"name" gorm:"column:name"` DbName string `json:"db_name" gorm:"column:db_name"` OnlineServiceName string `json:"online_service_name" gorm:"column:online_service_name"` InTime time.Time `json:"in_time" gorm:"autoCreateTime"` UpdateTime time.Time `json:"update_time" gorm:"autoUpdateTime"` // 公共属性 Pid int `json:"pid" gorm:"column:pid"` Bid int `json:"bid" gorm:"column:bid"` Mid int `json:"mid" gorm:"column:mid"` BusID string `json:"busid" gorm:"column:bus_id"` Biid string `json:"biid" gorm:"column:biid"` PublicKey string `json:"public_key" gorm:"column:public_key"` PrivateKey string `json:"private_key" gorm:"column:private_key"` ApiGetUrl string `json:"api_get_url" gorm:"column:api_get_url"` ApiPostUrl string `json:"api_post_url" gorm:"column:api_post_url"` JavaGetURL string `json:"java_get_url" gorm:"column:java_get_url"` PmuInfo string `json:"pmu_info" gorm:"column:pmu_info;size:2000"` AdaptorURL string `json:"adaptor_url" gorm:"column:adaptor_url"` NameServerURL string `json:"name_server_url" gorm:"column:name_server_url"` BotConfigs string `json:"bot_configs" gorm:"column:bot_configs"` } func (t *Project) TableName() string { return "project" } func main(){ db,_:=utils.GetHealthAIotDB() //db.Logger.LogMode() var list []Project db.Debug().Find(&list) fmt.Println(list) }
输出
2021/06/28 10:59:03 D:/go/src/awesomeProject2/main.go:43 [3.990ms] [rows:1] SELECT * FROM project [{1 0 2021-06-28 10:08:32.444365 +0800 CST 2021-06-28 10:08:32.444365 +0800 CST 0 0 0 }]
就你这个是怎么解决的呢。
type Project struct { ID int `json:"id" gorm:"column:id;primaryKey;autoIncrement"` Status int `json:"status" gorm:"column:status"` Product string `json:"product" gorm:"column:product"` Name string `json:"name" gorm:"column:name"` DbName string `json:"db_name" gorm:"column:db_name"` OnlineServiceName string `json:"online_service_name" gorm:"column:online_service_name"` InTime time.Time `json:"in_time" gorm:"autoCreateTime"` UpdateTime time.Time `json:"update_time" gorm:"autoUpdateTime"` // 公共属性 Pid int `json:"pid" gorm:"column:pid"` Bid int `json:"bid" gorm:"column:bid"` Mid int `json:"mid" gorm:"column:mid"` BusID string `json:"busid" gorm:"column:bus_id"` Biid string `json:"biid" gorm:"column:biid"` PublicKey string `json:"public_key" gorm:"column:public_key"` PrivateKey string `json:"private_key" gorm:"column:private_key"` ApiGetUrl string `json:"api_get_url" gorm:"column:api_get_url"` ApiPostUrl string `json:"api_post_url" gorm:"column:api_post_url"` JavaGetURL string `json:"java_get_url" gorm:"column:java_get_url"` PmuInfo string `json:"pmu_info" gorm:"column:pmu_info;size:2000"` AdaptorURL string `json:"adaptor_url" gorm:"column:adaptor_url"` NameServerURL string `json:"name_server_url" gorm:"column:name_server_url"` BotConfigs string `json:"bot_configs" gorm:"column:bot_configs"` } func (t *Project) TableName() string { return "project" } func main(){ db,_:=utils.GetHealthAIotDB() //db.Logger.LogMode() var list []Project db.Debug().Find(&list) fmt.Println(list) }
输出
2021/06/28 10:59:03 D:/go/src/awesomeProject2/main.go:43 [3.990ms] [rows:1] SELECT * FROM project [{1 0 2021-06-28 10:08:32.444365 +0800 CST 2021-06-28 10:08:32.444365 +0800 CST 0 0 0 }]
就你这个是怎么解决的呢。
你试试把所有column命名去掉,然后自动创建表试试。
我把gorm的tag都去掉了。也是用的gorm自动创建表。
那我就不知道了
输出