3

I've come across a peculiarity in GORM mapping.

What I expect table-wise is

models
designs (model_id -> models.id)
categories
categories_design (category_id, design_id)

To get a model_id in designs, I would use map syntax

static belongsTo = [model:Model]

So now, I also have a hasMany relationship between Design and Category and the owning side is Design.

The GORM manual says to denote an owning side using non-map syntax

static belongsTo = Category

BUT, I also need the back reference from Design to Model that uses map notation.

My question is how do you mix the 2 cases?

I have tried, in Design:

static belongsTo = [model:Model, category:Category]

This gets a runtime error - No owner defined between domain classes Category and Design in many-to-many relationship.

static belongsTo = [model:Model, Category]

This gives a compile error.

static belongsTo = [Model, Category] 

The expected model_id is not created in Design, instead a whole new table for models_designs is created which is not right. But everything compiles and runs.

PorridgeBear
  • 1,183
  • 1
  • 12
  • 19

1 Answers1

0

If you have a hasMany relation between Design and Category and Design is the owning side, should you not have a

static hasMany = [category: Category] in Design class 

and

static belongsTo = Design in Category class.

In this way, you can still keep

static belongsTo = [model:Model]

in the Design class.

RRK
  • 465
  • 2
  • 7
  • 21
  • No, because there the relationship I need is Design many-to-many Category. A M2M needs hasMany on both sites and a belongsTo on one side. But when there are multiple M2M relationships with other classes, the belongsTo syntax breaks down. Or I am just not seeing it. – PorridgeBear Nov 08 '12 at 09:50