MCPcopy Create free account
hub / github.com/go-pg/pg / ExampleDB_Model_insertOnConflictDoUpdate

Function ExampleDB_Model_insertOnConflictDoUpdate

example_model_test.go:173–204  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

171}
172
173func ExampleDB_Model_insertOnConflictDoUpdate() {
174 db := modelDB()
175
176 var book *Book
177 for i := 0; i < 2; i++ {
178 book = &Book{
179 ID: 100,
180 Title: fmt.Sprintf("title version #%d", i),
181 }
182 _, err := db.Model(book).
183 OnConflict("(id) DO UPDATE").
184 Set("title = EXCLUDED.title").
185 Insert()
186 if err != nil {
187 panic(err)
188 }
189
190 err = db.Model(book).WherePK().Select()
191 if err != nil {
192 panic(err)
193 }
194 fmt.Println(book)
195 }
196
197 _, err := db.Model(book).WherePK().Delete()
198 if err != nil {
199 panic(err)
200 }
201
202 // Output: Book<Id=100 Title="title version #0">
203 // Book<Id=100 Title="title version #1">
204}
205
206func ExampleDB_Model_selectOrInsert() {
207 db := modelDB()

Callers

nothing calls this directly

Calls 8

modelDBFunction · 0.85
InsertMethod · 0.80
SetMethod · 0.80
OnConflictMethod · 0.80
WherePKMethod · 0.80
DeleteMethod · 0.80
ModelMethod · 0.65
SelectMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…