go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.02k stars 3.94k forks source link

多对多创建的时候如何取消掉被关联表的ON DUPLICATE KEY UPDATE ? #7256

Open liyuan1125 opened 3 weeks ago

liyuan1125 commented 3 weeks ago

Your Question

type (
    Languages struct {
        ID   uint   `json:"id"`
        Name string `json:"name"`
    }

    User struct {
        ID        uint         `json:"id"`
        Name      string       `json:"name"`
        Languages []*Languages `json:"languages"`
    }
)

user := &User{Name: "jinzhu", Languages: []*Languages{
        {ID:1},
        {ID:2},
    }}

    db.Create(user)
    db.Session(&gorm.Session{FullSaveAssociations: false}).Create(user)

以上会多一句:

INSERT INTO `languages ` (`name`,`id`) VALUES ('',1),('',2) ON DUPLICATE KEY UPDATE `id`=`id`

不能跳过吗?

alex-guoba commented 2 weeks ago

omit the field