Grails

Many-to-Many Mapping generated wrong column names for association

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Not A Bug
  • Affects Version/s: 1.0.2
  • Fix Version/s: None
  • Component/s: Persistence
  • Labels:
    None

Description

The user guide (5.5.2.1 section Many-to-Many Mapping) states that the default names for

class Group {
...
static hasMany = [people:Person]
}
class Person {
...
static belongsTo = Group
static hasMany = [groups:Group]
}

will be person_id and group_id.

This is not the case. The name of the colelctions are used for the column name. In this case the names will be: people_id and groups_id.

Or this is a bug or the documentation is wrong.

I prefer the naming as described in the documentation.

Issue Links

Activity

Hide
Marcel Overdijk added a comment -

Note that you can use the custom ORM mappings to workaround this:

class Group {
...
static mapping = { people column:'person_id' }
}
class Person {
...
static mapping = { groups column:'group_id' }
}

Show
Marcel Overdijk added a comment - Note that you can use the custom ORM mappings to workaround this: class Group { ... static mapping = { people column:'person_id' } } class Person { ... static mapping = { groups column:'group_id' } }
Hide
Marcel Overdijk added a comment -

I changed this to critical as I found some other problem related to the naming of the association columns.

class Actor {

static belongsTo = Film
static hasMany = [films:Film]

String firstName
String lastName

static constraints = { firstName(maxSize:45) lastName(maxSize:45) }
}
class Film {

static hasMany = [actors:Actor]

String title
String description
Integer releaseYear
Integer rentalDuration
BigDecimal rentalRate
Integer length
BigDecimal replacementCost
String rating

static constraints = { title(maxSize:255) description(blank:true, nullable:true, maxSize:65535) releaseYear(nullable:true, range:1900..2100) rentalDuration(min:1, max:127) rentalRate(min:0.00, max:99.00, scale:2) length(nullable:true, min:1, max:32767) replacementCost(min:0.00, max:999.00, scale:2) rating(inList:["G", "PG", "PG-13", "R", "NC-17"]) }

}

When you have the domain classes as above Grails will automatically create the film_actor table containing:

  • actors_id
  • films_id

Now the critical part is that it appears that actors_id references the film table and films_id references the actor table... Should be the other way around I think.

Show
Marcel Overdijk added a comment - I changed this to critical as I found some other problem related to the naming of the association columns. class Actor { static belongsTo = Film static hasMany = [films:Film] String firstName String lastName static constraints = { firstName(maxSize:45) lastName(maxSize:45) } } class Film { static hasMany = [actors:Actor] String title String description Integer releaseYear Integer rentalDuration BigDecimal rentalRate Integer length BigDecimal replacementCost String rating static constraints = { title(maxSize:255) description(blank:true, nullable:true, maxSize:65535) releaseYear(nullable:true, range:1900..2100) rentalDuration(min:1, max:127) rentalRate(min:0.00, max:99.00, scale:2) length(nullable:true, min:1, max:32767) replacementCost(min:0.00, max:999.00, scale:2) rating(inList:["G", "PG", "PG-13", "R", "NC-17"]) } } When you have the domain classes as above Grails will automatically create the film_actor table containing:
  • actors_id
  • films_id
Now the critical part is that it appears that actors_id references the film table and films_id references the actor table... Should be the other way around I think.
Hide
Marcel Overdijk added a comment -

PS again the custom ORM mapping could be used to fix it:

class Actor {
...
static mapping = { films column:'actor_id' }
}

class Film {
...
static mapping = { actors column:'film_id' }
}

Show
Marcel Overdijk added a comment - PS again the custom ORM mapping could be used to fix it: class Actor { ... static mapping = { films column:'actor_id' } } class Film { ... static mapping = { actors column:'film_id' } }
Hide
Marcel Overdijk added a comment -

Should have looked on user forum and JIRA first.

Show
Marcel Overdijk added a comment - Should have looked on user forum and JIRA first.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: