what is the right way to do existence checking before insert ?
query the record and insert if no such record?
seems no insert or update support.
and the NewRecord method doesn't work as expected.
p := &Product{name: "hello"} // name is the primary key
NewRecord will return true even no such record in database.
BR.
I think the .Save() method has this semantics. If you save an existing object, it is getting updated. If you save a new object it is going to be creating.
Please test your self, I might be wrong, just remember from my last project.
I have some code like:
func (s PluginStore) CreateOrUpdate(plugin *model.Plugin) error {
err := s.db.Save(plugin).Error
return err
}
Yes, you're right!
Thank you!
Refer FirstOrCreate http://jinzhu.me/gorm/crud.html#firstorcreate
FirstOrCreate doesn't support for zero values of datatype
Most helpful comment
I think the
.Save()method has this semantics. If you save an existing object, it is getting updated. If you save a new object it is going to be creating.Please test your self, I might be wrong, just remember from my last project.
I have some code like: