在我的结构中,我有以下内容
type Task struct {
gorm.Model
Id int `json:"id" gorm:"primaryKey;AUTO_INCREMENT"`
UserId int `json:"user_id" gorm:"Index;not null" validate:"required"`
TaskId int `json:"task_id" gorm:"Index;not null" validate:"required"`
JobId int `json:"job_id" gorm:"not null" validate:"required"`
Latitude float64 `json:"latitude" gorm:"not null" validate:"required"`
Longitude float64 `json:"longitude" gorm:"not null" validate:"required"`
StartAt time.Time `json:"start_at"`
EndAt time.Time `json:"end_at"`
CreatedAt time.Time
UpdatedAt time.Time
}
我有这个功能可以用以下内容保存到表中
{ "user_id": 1,
"location":[5748.5445, 89790.454],
"latitude": 89790.454,
"longitude": 5748.5445,
"startAt": "2030-10-30T10:58:00.000Z",
"endAt": "2031-10-30T10:58:00.000Z"
}
func CreateTask(c *fiber.Ctx) error {
var opentask models.JobOpenTask
if err := c.BodyParser(&opentask); err != nil {
return c.Status(400).JSON(err.Error())
}
db.DB.Db.Create(&opentask)
return c.Status(200).JSON("OK")
}
当它运行时,它仍然将记录保存在 DB 上,但我希望它在尝试保存时会抛出错误,因为它在not null我的结构中,但为什么它能够保存到 Db 而不会抛出错误?
胡子哥哥
九州编程
慕桂英4014372
相关分类