Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 7, 2020
1 parent c2fab67 commit ce8bb06
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions example_many2many_test.go
Expand Up @@ -27,31 +27,6 @@ type OrderToItem struct {
ItemId int
}

func createManyToManyTables(db *pg.DB) error {
models := []interface{}{
(*Order)(nil),
(*Item)(nil),
(*OrderToItem)(nil),
}
for _, model := range models {
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: true,
})
if err != nil {
return err
}
}
return nil
}

// go-pg default convention is that:
// - Primary key is called Id, e.g. Model1.Id and Model2.Id.
// - Many to many table has columns Model1Id and Model2Id.
//
// If you are not using that convention you have 2 options:
// 1. Use orm.RegisterTable to register m2m table so go-pg has a chance
// to adopt to your convention.
// 2. Use `pg:fk:model2_id,joinFK:model1_id` to specify columns.
func ExampleDB_Model_manyToMany() {
db := connect()
defer db.Close()
Expand Down Expand Up @@ -113,3 +88,20 @@ func ExampleDB_Model_manyToMany() {
// Output: Order 1 Items 1 2
// Order 1 Items 2 1
}

func createManyToManyTables(db *pg.DB) error {
models := []interface{}{
(*Order)(nil),
(*Item)(nil),
(*OrderToItem)(nil),
}
for _, model := range models {
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: true,
})
if err != nil {
return err
}
}
return nil
}

0 comments on commit ce8bb06

Please sign in to comment.