Skip to content

Commit

Permalink
Merge pull request #1749 from martoche/many2many-multi-tenant-fix
Browse files Browse the repository at this point in the history
fix many2many for multi-tenant applications (table names with a param)
  • Loading branch information
vmihailenco authored Sep 27, 2020
2 parents c073c96 + ca8f1b1 commit 55b0ab7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
73 changes: 73 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

func init() {
orm.RegisterTable((*BookGenre)(nil))
orm.RegisterTable((*IngredientRecipe)(nil))
}

func TestGinkgo(t *testing.T) {
Expand Down Expand Up @@ -2429,3 +2430,75 @@ var _ = Describe("soft delete with int column", func() {
assert()
})
})

type Recipe struct {
tableName struct{} `pg:"?tenant.recipes"`
Id int
Ingredients []*Ingredient `pg:"many2many:?tenant.ingredients_recipes"`
}

type Ingredient struct {
tableName struct{} `pg:"?tenant.ingredients"`
Id int
Recipes []*Recipe `pg:"many2many:?tenant.ingredients_recipes"`
}

type IngredientRecipe struct {
tableName struct{} `pg:"?tenant.ingredients_recipes"`
Recipe *Recipe `pg:"rel:has-one"`
RecipeId int `pg:",pk"`
Ingredient *Ingredient `pg:"rel:has-one"`
IngredientId int `pg:",pk"`
}

var _ = Describe("many2many multi-tenant bug", func() {
var db *pg.DB

BeforeEach(func() {
db = testDB().WithParam("tenant", pg.Safe("public"))
options := orm.CreateTableOptions{}

err := db.Model((*Recipe)(nil)).CreateTable(&options)
Expect(err).NotTo(HaveOccurred())

err = db.Model((*Ingredient)(nil)).CreateTable(&options)
Expect(err).NotTo(HaveOccurred())

err = db.Model((*IngredientRecipe)(nil)).CreateTable(&options)
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
err := db.Model((*Recipe)(nil)).DropTable(nil)
Expect(err).NotTo(HaveOccurred())

err = db.Model((*Ingredient)(nil)).DropTable(nil)
Expect(err).NotTo(HaveOccurred())

err = db.Model((*IngredientRecipe)(nil)).DropTable(nil)
Expect(err).NotTo(HaveOccurred())
})

It("should find the many2many table", func() {
recipe := Recipe{Id: 1}
ingredient := Ingredient{Id: 1}
ingredientRecipe := IngredientRecipe{
RecipeId: 1,
IngredientId: 1,
}

_, err := db.Model(&recipe).Insert()
Expect(err).NotTo(HaveOccurred())

_, err = db.Model(&ingredient).Insert()
Expect(err).NotTo(HaveOccurred())

_, err = db.Model(&ingredientRecipe).Insert()
Expect(err).NotTo(HaveOccurred())

err = db.Model(&recipe).WherePK().Relation("Ingredients").Select()
Expect(err).NotTo(HaveOccurred())
Expect(recipe.Ingredients).To(HaveLen(1))
Expect(recipe.Ingredients[0].Id).To(Equal(1))
})
})
2 changes: 1 addition & 1 deletion orm/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ func (t *Table) mustM2MRelation(field *Field, pgTag *tagparser.Tag) bool {
if !ok {
panic(fmt.Errorf("pg: %s must have many2many tag option", field.GoName))
}
m2mTableName := quoteIdent(m2mTableNameString)
m2mTableName := quoteTableName(m2mTableNameString)

m2mTable := _tables.getByName(m2mTableName)
if m2mTable == nil {
Expand Down

0 comments on commit 55b0ab7

Please sign in to comment.