@@ -23,6 +23,7 @@ import (
23
23
24
24
func init () {
25
25
orm .RegisterTable ((* BookGenre )(nil ))
26
+ orm .RegisterTable ((* IngredientRecipe )(nil ))
26
27
}
27
28
28
29
func TestGinkgo (t * testing.T ) {
@@ -2429,3 +2430,75 @@ var _ = Describe("soft delete with int column", func() {
2429
2430
assert ()
2430
2431
})
2431
2432
})
2433
+
2434
+ type Recipe struct {
2435
+ tableName struct {} `pg:"?tenant.recipes"`
2436
+ Id int
2437
+ Ingredients []* Ingredient `pg:"many2many:?tenant.ingredients_recipes"`
2438
+ }
2439
+
2440
+ type Ingredient struct {
2441
+ tableName struct {} `pg:"?tenant.ingredients"`
2442
+ Id int
2443
+ Recipes []* Recipe `pg:"many2many:?tenant.ingredients_recipes"`
2444
+ }
2445
+
2446
+ type IngredientRecipe struct {
2447
+ tableName struct {} `pg:"?tenant.ingredients_recipes"`
2448
+ Recipe * Recipe `pg:"rel:has-one"`
2449
+ RecipeId int `pg:",pk"`
2450
+ Ingredient * Ingredient `pg:"rel:has-one"`
2451
+ IngredientId int `pg:",pk"`
2452
+ }
2453
+
2454
+ var _ = Describe ("many2many multi-tenant bug" , func () {
2455
+ var db * pg.DB
2456
+
2457
+ BeforeEach (func () {
2458
+ db = testDB ().WithParam ("tenant" , pg .Safe ("public" ))
2459
+ options := orm.CreateTableOptions {}
2460
+
2461
+ err := db .Model ((* Recipe )(nil )).CreateTable (& options )
2462
+ Expect (err ).NotTo (HaveOccurred ())
2463
+
2464
+ err = db .Model ((* Ingredient )(nil )).CreateTable (& options )
2465
+ Expect (err ).NotTo (HaveOccurred ())
2466
+
2467
+ err = db .Model ((* IngredientRecipe )(nil )).CreateTable (& options )
2468
+ Expect (err ).NotTo (HaveOccurred ())
2469
+ })
2470
+
2471
+ AfterEach (func () {
2472
+ err := db .Model ((* Recipe )(nil )).DropTable (nil )
2473
+ Expect (err ).NotTo (HaveOccurred ())
2474
+
2475
+ err = db .Model ((* Ingredient )(nil )).DropTable (nil )
2476
+ Expect (err ).NotTo (HaveOccurred ())
2477
+
2478
+ err = db .Model ((* IngredientRecipe )(nil )).DropTable (nil )
2479
+ Expect (err ).NotTo (HaveOccurred ())
2480
+ })
2481
+
2482
+ It ("should find the many2many table" , func () {
2483
+ recipe := Recipe {Id : 1 }
2484
+ ingredient := Ingredient {Id : 1 }
2485
+ ingredientRecipe := IngredientRecipe {
2486
+ RecipeId : 1 ,
2487
+ IngredientId : 1 ,
2488
+ }
2489
+
2490
+ _ , err := db .Model (& recipe ).Insert ()
2491
+ Expect (err ).NotTo (HaveOccurred ())
2492
+
2493
+ _ , err = db .Model (& ingredient ).Insert ()
2494
+ Expect (err ).NotTo (HaveOccurred ())
2495
+
2496
+ _ , err = db .Model (& ingredientRecipe ).Insert ()
2497
+ Expect (err ).NotTo (HaveOccurred ())
2498
+
2499
+ err = db .Model (& recipe ).WherePK ().Relation ("Ingredients" ).Select ()
2500
+ Expect (err ).NotTo (HaveOccurred ())
2501
+ Expect (recipe .Ingredients ).To (HaveLen (1 ))
2502
+ Expect (recipe .Ingredients [0 ].Id ).To (Equal (1 ))
2503
+ })
2504
+ })
0 commit comments