Search code examples
grailshas-manyhas-one

Grails hasOne and hasMany same domain


I have domain like this:

class Team {
 hasOne [leader: Person]
 hasMany [member: Person]
}

class Person {
 belongsTo [team: Team]
}

But when the tables are generated, there is not a column like leader_id in the team table. And thus the leader relation is not persisted.

How should I fix it?


Solution

  • I figured that, what I need is

    class Team {
     belongsTo [leader: Person]
     hasMany [member: Person]
    }
    
    class Person {
     belongsTo [team: Team]
    }
    

    so that the Team table can have the desired "leader" reference back to Person.