phauwn
phauwn

Reputation: 366

Grails many to many relationship not creating expected tables

Hi I have 2 domain classes. The idea is that a User can create events, as well as be a guest at other peoples events:

class Event {

Appuser creator
static belongsTo = Appuser
static hasMany = [guests: Appuser]

and

class Appuser {

    static hasMany = [friends: Appuser, events: Event]

The problem is, I expected it to make a creator_id column in the event table, and then an appuser_events table with just the appuser_id and the event_id, but it is also including a creator_id column here and making it the primary key.

I also tried creating my event class like this:

class Event {


    static belongsTo = [creator:Appuser]
    static hasMany = [guests: Appuser]

but then grails does not recognize the belongTo relationship.

Any idea how to fix?

Upvotes: 0

Views: 194

Answers (1)

phauwn
phauwn

Reputation: 366

Ok, I solved my issue. The key was to add 2 hasMany references to the Event class from Appuser, and then add a mappedBy, so my Event Class now contains:

static hasMany = [friends: Appuser, events: Event, invites:Event]
static mappedBy = [invites: "guests", events: "creator"]

The Appuser class is unchanged from my first example in the question.

Upvotes: 1

Related Questions