Your issue may already be reported! Please search on the issue track before creating one.
What version of Go are you using (go version)?
go version go1.14 linux/amd64
Which database and its version are you using?
Mysql 8.0.20
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 (
"fmt"
"log"
"os"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
)
type User struct {
Id int
Name string
Address Address `gorm:"ForeignKey:AddressID;References:id"`
AddressID int
Date *time.Time
}
type Address struct {
Id int
Address1 string
}
var Default = logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: 100 * time.Millisecond,
LogLevel: logger.Info,
Colorful: true,
})
func main() {
dsn := "root:password@tcp(localhost:3306)/db?charset=utf8&parseTime=True&loc=Local"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: Default})
_ = db.AutoMigrate(&User{}, &Address{})
now := time.Now()
users := []User{{Name: "mark", Date: &now, Address: Address{Address1: "address 1"}},
{Name: "jhon", Date: &now, Address: Address{Address1: "address 2"}}}
err := db.Create(&users).Error
fmt.Println(err)
//Upsert
users = []User{{Id: 1, Name: "juan"}, {Id: 2, Name: "pedro"}}
err = db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.Assignments(map[string]interface{}{"name": "????"}), //<-- How update 1->juan, 2->jhon
}).Create(&users).Error
fmt.Println(err)
}
I am trying to update the record with ID 1 with the name "juan" and the record with ID 2 with the name pedro, how can I do it?
**Comment From: jinzhu**
> I am trying to update the record with ID 1 with the name "juan" and the record with ID 2 with the name pedro, how can I do it?
We can't support update a column to different values when batch insert due to SQL limitation ;(
**Comment From: jinzhu**
But you can do a batch update like this:
https://www.tutorialspoint.com/mysql-mass-update-with-case-when-then-else
It would be possible to make GORM support this if it is what you want...
**Comment From: efrengarcial**
> We can't support update a column to different values when batch insert due to SQL limitation ;(
Excuse my ignorance, it is possible to generate the SQL this way:
Mysql case
INSERT INTO `users` (`name`,`address_id`,`date`,`id`) VALUES ("juan",0,NULL,1),("pedro",0,NULL,2) ON DUPLICATE KEY UPDATE `name`=**values(name)**
Postgres and Sqlite cases
INSERT INTO `users` (`name`,`address_id`,`date`,`id`) VALUES ("juan",0,NULL,1),("pedro",0,NULL,2) ON CONFLICT (id) DO UPDATE SET `name`=**excluded.name**
**Comment From: jinzhu**
Good to know, let me think how to make the API works with that.
**Comment From: jinzhu**
```go
db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]{"name"}),
}).Create(&users).Error
Comment From: efrengarcial
works fine thanks a lot.