そのような場合は
gorm:association_autoupdate
を
false
に設定することができます。
db.Set("gorm:association_autoupdate", false).Create(&user) db.Set("gorm:association_autoupdate", false).Save(&user)
|
あるいは
gorm:"association_autoupdate:false"
タグを使用します。
type User struct { gorm.Model Name string CompanyID uint Company Company `gorm:"association_autoupdate:false"` }
|
これを無効にするには、DB設定のgorm:association_autocreateをfalseに設定します
db.Set("gorm:association_autoupdate", false).Create(&user) db.Set("gorm:association_autoupdate", false).Save(&user)
|
または、GORMタグのgorm:”association_autocreate:false”を使用します。
type User struct {
gorm.Model
Name string
// Don't create associations w/o primary key, WON'T save its reference
Company1 Company `gorm:"association_autocreate:false"`
}
db.Set("gorm:association_autoupdate", false).Set("gorm:association_autocreate", false).Create(&user)
type User struct { gorm.Model Name string Company Company `gorm:"association_autoupdate:false;association_autocreate:false"` }
|
もしくは、
gorm:save_associations
タグを使用します。
db.Set("gorm:save_associations", false).Create(&user)
db.Set("gorm:save_associations", false).Save(&user)
type User struct {
gorm.Model
Name string
Company Company `gorm:"save_associations:false"`
}