-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
example_composite_test.go
59 lines (46 loc) · 1.04 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/v10/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.Model((*OnHand)(nil)).DropTable(&orm.DropTableOptions{
IfExists: true,
Cascade: true,
})
panicIf(err)
err = db.Model((*InventoryItem)(nil)).DropComposite(&orm.DropCompositeOptions{
IfExists: true,
})
panicIf(err)
err = db.Model((*InventoryItem)(nil)).CreateComposite(nil)
panicIf(err)
err = db.Model((*OnHand)(nil)).CreateTable(nil)
panicIf(err)
_, err = db.Model(&OnHand{
Item: InventoryItem{
Name: "fuzzy dice",
SupplierID: 42,
Price: 1.99,
},
Count: 1000,
}).Insert()
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
}