I’m trying to create a very simple relationship between two objects. Can anybody explain me why I can’t find the Company object via findBy method?
class Company {
String name
String desc
City city
static constraints = {
city(unique: true)
}
}
class City {
String name
static constraints = {
}
}
class BootStrap {
def init = { servletContext ->
new City(name: 'Tokyo').save()
new City(name: 'New York').save()
new Company(name: 'company', city: City.findByName('New York')).save()
def c = Company.findByName('company') // Why c=null????!
}
def destroy = {
}
}
A field called
descconflicts with the database keyword for descending sort. Per default a field isnullable:falsein Grails. So first rename that field to for exampledescriptionand then provide one or mark that field asnullable:truein your constraints.Remember that you can always check for the errors that prevent Grails from saving your objects to the database easily: