添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

i have two model (one base model and one database model) . i want to update status (Isactive field) and other some fields but when i send status = false then gorm not send true sql query. if i send status = true then it is true running.

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	"log"
	"time"
type BaseModel struct {
	ID        uint64     `json:"-" gorm:"primary_key AUTO_INCREMENT"`
	CreatedAt time.Time  `json:"-,string" gorm:"not null;column:created_at"`
	UpdatedAt time.Time  `json:"-" gorm:"not null;column:updated_at"`
	DeletedAt *time.Time `json:"-" sql:"index;column:deleted_at"`
	IsActive  bool       `json:"is_active" sql:"DEFAULT:true;index" gorm:"not null"`
func (model *BaseModel) BeforeSave(scope *gorm.Scope) error {
	model.IsActive = true
	model.CreatedAt = time.Now()
	model.UpdatedAt = time.Now()
	return nil
type ApplicationModel struct {
	BaseModel
	Name    string `gorm:"not null;size:255;not null" validate:"required"`
	Version string `gorm:"not null;size:100;not null" validate:"required"`
func (*ApplicationModel) TableName() string {
	return "applications"
func main() {
	database, err := gorm.Open("postgres", "host=localhost port=5432 user=postgres password=postgres dbname=ldms sslmode=disable")
	database.LogMode(true)
	if err != nil {
		log.Println("Could not connect database")
		log.Fatal(err)
	defer database.Close()
	application := &ApplicationModel{BaseModel: BaseModel{ID: 71}}
	updatedApplicationCol := &ApplicationModel{BaseModel{ IsActive: false}, "TEST", "v1.1.1.1"}
	database.Model(application).Update(updatedApplicationCol)
	fmt.Println("Done...")

UPDATE "applications" SET "name" = 'TEST', "updated_at" = '2018-09-29 21:33:45', "version" = 'v1.1.1.1' WHERE "applications"."deleted_at" IS NULL AND "applications"."id" = '71'

How can update status field ?

i found answer

// WARNING when update with struct, GORM will only update those fields that with non blank value
// For below Update, nothing will be updated as "", 0, false are blank values of their types
db.Model(&user).Updates(User{Name: "", Age: 0, Actived: false})

Now firstly i convert model to map and after update with map. Roman Verteletsky <[email protected]>, 22 Kas 2018 Per, 19:53 tarihinde şunu yazdı: