Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dateCreated field to join table in Grails

I have a M:M relationship between User and Badge which creates a join table called "user_badges". This table has the fields: user_id and badge_id. Is there a way to get the standard date_created fields on this table?

class Badge {

static belongsTo = User
static hasMany = [users: User]
}

class User {
 static hasMany = [badges: Badge]
}
like image 643
RyanLynch Avatar asked Dec 29 '22 01:12

RyanLynch


2 Answers

Basically, you need to change the mapping so that the M:M relationship is expressed as two 1:M relationships. Here's an example where the joining class is BadgeOwner (so by default the generated join table will be named badge_owner)

class Badge {    
  static hasMany = [owners: BadgeOwner]
}

class User {
  static hasMany = [owners: BadgeOwner]
}

class BadgeOwner {
  static belongsTo = [user: User, badge: Badge]
  Date dateCreated 
  Date lastUpdated
}
like image 200
Dónal Avatar answered Jan 06 '23 10:01

Dónal


If it has additional properties, it's not a join table. It's a separate entity. So, map it accordingly :-)

like image 33
jpkrohling Avatar answered Jan 06 '23 10:01

jpkrohling