Dany Y
Dany Y

Reputation: 7041

grails derived property creating column in database

I'm using grails 2.4.4.

I have the domain

class Post {
    Integer nbrOfFavorites
    static hasMany = [
        favorites : Favorite
    ]

    static mappings = {
        nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
    }
}

The problem is that nbrOfFavorites is being created in the database, so retrieving it doesn't take into account the formula.

Is there something wrong in my syntax ?

Thanks

Upvotes: 2

Views: 701

Answers (2)

MKB
MKB

Reputation: 7619

Yes there is a typo in the syntax. Change mappings to mapping, like:

static mapping = {
    nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
}

Ref# Grails Domain: mapping

Upvotes: 3

WeMakeSoftware
WeMakeSoftware

Reputation: 9162

try adding this field as a transient to the domain object:

class Post {
Integer nbrOfFavorites
static hasMany = [
    favorites : Favorite
]

static mappings = {
    nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
}

static transients = ['nbrOfFavorites']
}

Upvotes: 0

Related Questions