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

Function ExampleDB_Model_bulkUpdateSlice

example_model_test.go:927–957  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

925}
926
927func ExampleDB_Model_bulkUpdateSlice() {
928 db := modelDB()
929
930 books := []Book{{
931 ID: 1,
932 Title: "updated book 1",
933 UpdatedAt: time.Now(),
934 }, {
935 ID: 2,
936 Title: "updated book 2",
937 UpdatedAt: time.Now(),
938 }}
939
940 // UPDATE "books" AS "book"
941 // SET "title" = _data."title"
942 // FROM (VALUES ('updated book 1', 1), ('updated book 2', 2)) AS _data("title", "id")
943 // WHERE "book"."id" = _data."id"
944 _, err := db.Model(&books).Column("title", "updated_at").Update()
945 if err != nil {
946 panic(err)
947 }
948
949 books = nil
950 err = db.Model(&books).Order("id").Select()
951 if err != nil {
952 panic(err)
953 }
954
955 fmt.Println(books)
956 // Output: [Book<Id=1 Title="updated book 1"> Book<Id=2 Title="updated book 2"> Book<Id=3 Title="book 3">]
957}
958
959func ExampleDB_Model_delete() {
960 db := modelDB()

Callers

nothing calls this directly

Calls 6

modelDBFunction · 0.85
UpdateMethod · 0.80
ColumnMethod · 0.80
OrderMethod · 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…