golang gin gorm 插入并设置 primary_key 但 primary_key 为空
golang gin gorm insert and set primary_key but primary_key got null
我使用 gin gorm mysql 构建应用程序。
我在 model.go 中设置 topic_id primary_key auto_increment not null 如下:
type Topic struct {
gorm.Model
TopicId uint64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT;NOT NULL"`
TopicName string
TopicDesc string
OwnerId int
CreateIP string
CreateTime uint64
UpdateTime uint64
}
在 service.go
中创建主题
type TopicCreateService struct{
TopicName string `form:"topic_name" json:"topic_name" binding:"required,min=1,max=30"`
TopicDesc string `form:"topic_desc" json:"topic_desc" binding:"required,min=1,max=300"`
OwnerId int `form:"owner_id" json:"owner_id" binding:"required,min=1,max=30"`
}
func (service *TopicCreateService) Create(c *gin.Context) serializer.Response{
topic := model.Topic{
TopicName:service.TopicName,
TopicDesc:service.TopicDesc,
OwnerId:service.OwnerId,
CreateIP:c.ClientIP(),
CreateTime:uint64(time.Now().UnixNano()),
UpdateTime:0,
}
if err:=model.DB.Create(&topic).Error;err!=nil{
return serializer.ParamErr("创建话题失败", err)
}
return serializer.BuildTopicResponse(topic)
}
我希望 topic_id 成为我的 primary_key 而不是 null 自动递增。怎么了?
您已将 gorm.Model
包含在您的结构中。这意味着你的模型 migration/database 会报错:
Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key
如果您从 Topic
结构中删除 gorm.Model
,您会很好。
package model
import (
`github.com/jinzhu/gorm`
)
type WithoutModel struct {
MyId int64 `gorm:"primary_key;auto_increment;not_null"`
Name string
}
func ModelSave(tx *gorm.DB) {
wo := WithoutModel{Name:"Without the model"}
tx.Save(&wo)
}
在运行 ModelSave
几次之后,我有:
MariaDB [gmodel]> select * from without_models;
+-------+-------------------+
| my_id | name |
+-------+-------------------+
| 1 | Without the model |
| 2 | Without the model |
+-------+-------------------+
2 rows in set (0.000 sec)
gorm.Model
// gorm.Model 定义
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
我使用 gin gorm mysql 构建应用程序。
我在 model.go 中设置 topic_id primary_key auto_increment not null 如下:
type Topic struct {
gorm.Model
TopicId uint64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT;NOT NULL"`
TopicName string
TopicDesc string
OwnerId int
CreateIP string
CreateTime uint64
UpdateTime uint64
}
在 service.go
中创建主题type TopicCreateService struct{
TopicName string `form:"topic_name" json:"topic_name" binding:"required,min=1,max=30"`
TopicDesc string `form:"topic_desc" json:"topic_desc" binding:"required,min=1,max=300"`
OwnerId int `form:"owner_id" json:"owner_id" binding:"required,min=1,max=30"`
}
func (service *TopicCreateService) Create(c *gin.Context) serializer.Response{
topic := model.Topic{
TopicName:service.TopicName,
TopicDesc:service.TopicDesc,
OwnerId:service.OwnerId,
CreateIP:c.ClientIP(),
CreateTime:uint64(time.Now().UnixNano()),
UpdateTime:0,
}
if err:=model.DB.Create(&topic).Error;err!=nil{
return serializer.ParamErr("创建话题失败", err)
}
return serializer.BuildTopicResponse(topic)
}
我希望 topic_id 成为我的 primary_key 而不是 null 自动递增。怎么了?
您已将 gorm.Model
包含在您的结构中。这意味着你的模型 migration/database 会报错:
Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key
如果您从 Topic
结构中删除 gorm.Model
,您会很好。
package model
import (
`github.com/jinzhu/gorm`
)
type WithoutModel struct {
MyId int64 `gorm:"primary_key;auto_increment;not_null"`
Name string
}
func ModelSave(tx *gorm.DB) {
wo := WithoutModel{Name:"Without the model"}
tx.Save(&wo)
}
在运行 ModelSave
几次之后,我有:
MariaDB [gmodel]> select * from without_models;
+-------+-------------------+
| my_id | name |
+-------+-------------------+
| 1 | Without the model |
| 2 | Without the model |
+-------+-------------------+
2 rows in set (0.000 sec)
gorm.Model
// gorm.Model 定义
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}