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

Model(&user).Update("a",1), if user with association, if user with association,the association will be updated #2278

Open
mohuishou opened this issue Jan 29, 2019 · 1 comment

Comments

@mohuishou
Copy link

commented Jan 29, 2019

What version of Go are you using (go version)?

go version go1.11 windows/amd64

Which database and its version are you using?

mysql@8.0

Please provide a complete runnable program to reproduce your issue. IMPORTANT

Need to runnable with GORM's docker compose config or please provides your config.

package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mssql"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

var db *gorm.DB

func init() {
	var err error
	db, err = gorm.Open("sqlite3", "test.db")
	// db, err = gorm.Open("postgres", "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable")
	// db, err = gorm.Open("mysql", "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True")
	// db, err = gorm.Open("mssql", "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm")
	if err != nil {
		panic(err)
	}
	db.LogMode(true)
}

type Product struct {
	gorm.Model
	Code string
	Price uint
	Applications []Application
}

type Application struct {
	gorm.Model
	Name string
	ProductID uint
}

func main() {
		// Migrate the schema
	db.AutoMigrate(&Product{},&Application{})

	// Create
	db.Create(&Product{Code: "L1212", Price: 1000})

	// Read
	var product Product
	db.First(&product, 1) // find product with id 1
	product.Applications = []Application{
		{
			Name: "test",
		},
	}
	// Update - update product's price to 2000
	db.Model(&product).Update("Price", 1000)
       // 
}

in Doc

Update single attribute if it is changed
db.Model(&user).Update("name", "hello")

Expect

UPDATE products SET price = '1000', updated_at = '2019-01-29 21:58:52'  WHERE products.deleted_at IS NULL

Actual

UPDATE products SET price = '1000', updated_at = '2019-01-29 21:58:52'  WHERE products.deleted_at IS NULL
INSERT INTO applications (created_at,updated_at,deleted_at,name,product_id) VALUES ('2019-01-29 21:58:52','2019-01-29 21:58:52',NULL,'test','0')  
@mohuishou

This comment has been minimized.

Copy link
Author

commented Jan 29, 2019

I don't confirm that it's just a bug or a feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.