History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GRAILS-2922
Type: Bug Bug
Status: Closed Closed
Resolution: Not A Bug
Priority: Critical Critical
Assignee: Graeme Rocher
Reporter: Marcel Overdijk
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Grails

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

Created: 08/May/08 02:57 PM   Updated: 09/May/08 01:32 AM
Component/s: Persistence
Affects Version/s: 1.0.2
Fix Version/s: None

Time Tracking:
Not Specified

Issue Links:
Duplicate
 


 Description  « Hide
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.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Marcel Overdijk - 08/May/08 03:01 PM
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' }
}


Marcel Overdijk - 08/May/08 04:25 PM
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.


Marcel Overdijk - 08/May/08 04:30 PM
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' }
}


Marcel Overdijk - 09/May/08 01:32 AM
Should have looked on user forum and JIRA first.