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

Function ExampleDB_Model

example_db_model_test.go:31–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29}
30
31func ExampleDB_Model() {
32 db := pg.Connect(&pg.Options{
33 User: "postgres",
34 Password: "postgres",
35 })
36 defer db.Close()
37
38 err := createSchema(db)
39 if err != nil {
40 panic(err)
41 }
42
43 user1 := &User{
44 Name: "admin",
45 Emails: []string{"admin1@admin", "admin2@admin"},
46 }
47 _, err = db.Model(user1).Insert()
48 if err != nil {
49 panic(err)
50 }
51
52 _, err = db.Model(&User{
53 Name: "root",
54 Emails: []string{"root1@root", "root2@root"},
55 }).Insert()
56 if err != nil {
57 panic(err)
58 }
59
60 story1 := &Story{
61 Title: "Cool story",
62 AuthorId: user1.Id,
63 }
64 _, err = db.Model(story1).Insert()
65 if err != nil {
66 panic(err)
67 }
68
69 // Select user by primary key.
70 user := &User{Id: user1.Id}
71 err = db.Model(user).WherePK().Select()
72 if err != nil {
73 panic(err)
74 }
75
76 // Select all users.
77 var users []User
78 err = db.Model(&users).Select()
79 if err != nil {
80 panic(err)
81 }
82
83 // Select story and associated author in one query.
84 story := new(Story)
85 err = db.Model(story).
86 Relation("Author").
87 Where("story.id = ?", story1.Id).
88 Select()

Callers

nothing calls this directly

Calls 8

InsertMethod · 0.80
WherePKMethod · 0.80
WhereMethod · 0.80
createSchemaFunction · 0.70
CloseMethod · 0.65
ModelMethod · 0.65
RelationMethod · 0.65
SelectMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…