-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_composite_test.go
59 lines (46 loc) · 1.01 KB
/
example_composite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package pg_test
import (
"fmt"
"github.com/go-pg/pg/v9/orm"
)
type InventoryItem struct {
Name string
SupplierID int
Price float64
}
type OnHand struct {
tableName struct{} `pg:"on_hand"`
Item InventoryItem `pg:"composite:inventory_item"`
Count int
}
func ExampleDB_Model_compositeType() {
db := connect()
defer db.Close()
err := db.DropTable((*OnHand)(nil), &orm.DropTableOptions{
IfExists: true,
Cascade: true,
})
panicIf(err)
err = db.DropComposite((*InventoryItem)(nil), &orm.DropCompositeOptions{
IfExists: true,
})
panicIf(err)
err = db.CreateComposite((*InventoryItem)(nil), nil)
panicIf(err)
err = db.CreateTable((*OnHand)(nil), nil)
panicIf(err)
err = db.Insert(&OnHand{
Item: InventoryItem{
Name: "fuzzy dice",
SupplierID: 42,
Price: 1.99,
},
Count: 1000,
})
panicIf(err)
onHand := new(OnHand)
err = db.Model(onHand).Select()
panicIf(err)
fmt.Println(onHand.Item.Name, onHand.Item.Price, onHand.Count)
// Output: fuzzy dice 1.99 1000
}