Grails: does belongsTo create a new column for a back reference?

171 Views Asked by At

My code looks something like

class Person {
    Nose nose
}

class Nose {
    static belongsTo = [person:Person]
}

This is supposed to create a foreign key column for Nose on the 'person' table and also a back reference column for Person on the 'nose' table right?

There is no column generated for the back reference on the 'nose' table for me right now and I was wondering if this is normal...

http://grails.org/doc/latest/ref/Domain%20Classes/belongsTo.html (Seems like this is what the documentation is saying, but I'm thinking I'm interpreting it wrong)

edit: i edited the code snippet because i wrote the wrong thing down (haven't had coffee today)

2

There are 2 best solutions below

1
On

You should reference the Person in the Nose, not Nose itself, like this:

class Person {
    Nose nose
}

class Nose {
    static belongsTo = [person: Person]
}

EDIT AFTER COMMENT: ah, I see. Now that I am reading your question again, were you expecting to have both a person_id column in Nose and nose_id column in Person? Because that will never happen in GORM, at least not automatically - for your schema, only the Person table will contain a nose_id column per documentation.

6
On

In grails we use belongsTo for cascading purpose,You should use

class Nose {
belongsTo = [person:Person]
}

and yes in case of belongsTo a back reference will be stored in Nose having the person_id.