Gorm: no insert or update support?

Created on 8 Feb 2017  路  4Comments  路  Source: go-gorm/gorm

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.

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:

func (s PluginStore) CreateOrUpdate(plugin *model.Plugin) error {
    err := s.db.Save(plugin).Error
    return err
}

All 4 comments

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!

FirstOrCreate doesn't support for zero values of datatype

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corvinusy picture corvinusy  路  3Comments

sredxny picture sredxny  路  3Comments

hypertornado picture hypertornado  路  3Comments

fieryorc picture fieryorc  路  3Comments

bramp picture bramp  路  3Comments