vrghost
vrghost

Reputation: 1224

Grails: Saving a child record fails for already existing parent record

Have been struggling with this for a while now. What I am trying to do is to create a standard relationship in grails where I have parent records (hasMany), these records contain two columns (4 if we count id and version)

Defined as follows:

    class Country {
    static hasMany = [regstats: Regstat]
    String countryiso
    String countryname
    static constraints = {
        countryiso size:2..2, unique: true, validator:{ it.toUpperCase() == it }
    }
    static mapping = {
        index: 'countryiso' 
    }
}

This table is quite simple, it contains the iso3611 code of each country, and its full name.

Second one contains a bunch of data about the country, and I want it to have the child/parent relationship. IE, each country can have multiple records, but only one unique country/reg type may exist at any one time.

class Regstat {
    static belongsTo = [country: Country]
    String reg
    int status
    String exturl
    Date impdate
    Date lupdate
    String impnote
    static constraints = {
        reg(inList: ["FATCA", "ITC2014", "AEOI"], unique:'country')
        exturl(nullable:true)
        impnote(nullable:true)

    }
    static mapping = {
        index: 'reg'
        impnote type: 'text'
    }
}

I have a function that will load the country definitions from a csv file, all good, creates all 243 countries.

Then I try to create the child records (in a different function, the countries have been saved to the database, have checked).

Run the following:

                render ("Writing some stuff<BR>")
                render ("To be more precise: ${exturl} ${impdate} ${lupdate} ${impnote} ${ref} ${status} <BR>")
                def qp = Country.findByCountryname(cont)
                    qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, lupdate:lupdate,impnote:impnote,reg:ref,status:status))
                    qp.save(flush:true,failOnError:true)

And it falls over to no end, and I can't quite understand the output to be honest. (OK, first two lines was me adding something to see if the variables where empty.

Writing some stuff
To be more precise: N/A 2014-06-30 2014-09-29 FATCA 3 
Error 500: Internal Server Error
URI
/RegMap/countryupload/loadcsv
Class
grails.validation.ValidationException
Message
Validation Error(s) occurred during save(): - Field error in object 'regmap.Country' on field 'regstats[0].impdate': rejected value [null]; codes [regmap.Regstat.impdate.nullable.error.regmap.Country.regstats[0].impdate,regmap.Regstat.impdate.nullable.error.regmap.Country.regstats.impdate,regmap.Regstat.impdate.nullable.error.regstats[0].impdate,regmap.Regstat.impdate.nullable.error.regstats.impdate,regmap.Regstat.impdate.nullable.error.impdate,regmap.Regstat.impdate.nullable.error.java.util.Date,regmap.Regstat.impdate.nullable.error,regstat.impdate.nullable.error.regmap.Country.regstats[0].impdate,regstat.impdate.nullable.error.regmap.Country.regstats.impdate,regstat.impdate.nullable.error.regstats[0].impdate,regstat.impdate.nullable.error.regstats.impdate,regstat.impdate.nullable.error.impdate,regstat.impdate.nullable.error.java.util.Date,regstat.impdate.nullable.error,regmap.Regstat.impdate.nullable.regmap.Country.regstats[0].impdate,regmap.Regstat.impdate.nullable.regmap.Country.regstats.impdate,regmap.Regstat.impdate.nullable.regstats[0].impdate,regmap.Regstat.impdate.nullable.regstats.impdate,regmap.Regstat.impdate.nullable.impdate,regmap.Regstat.impdate.nullable.java.util.Date,regmap.Regstat.impdate.nullable,regstat.impdate.nullable.regmap.Country.regstats[0].impdate,regstat.impdate.nullable.regmap.Country.regstats.impdate,regstat.impdate.nullable.regstats[0].impdate,regstat.impdate.nullable.regstats.impdate,regstat.impdate.nullable.impdate,regstat.impdate.nullable.java.util.Date,regstat.impdate.nullable,nullable.regmap.Country.regstats[0].impdate,nullable.regmap.Country.regstats.impdate,nullable.regstats[0].impdate,nullable.regstats.impdate,nullable.impdate,nullable.java.util.Date,nullable]; arguments [impdate,class regmap.Regstat]; default message [Property [{0}] of class [{1}] cannot be null] - Field error in object 'regmap.Country' on field 'regstats[0].lupdate': rejected value [null]; codes [regmap.Regstat.lupdate.nullable.error.regmap.Country.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.error.regmap.Country.regstats.lupdate,regmap.Regstat.lupdate.nullable.error.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.error.regstats.lupdate,regmap.Regstat.lupdate.nullable.error.lupdate,regmap.Regstat.lupdate.nullable.error.java.util.Date,regmap.Regstat.lupdate.nullable.error,regstat.lupdate.nullable.error.regmap.Country.regstats[0].lupdate,regstat.lupdate.nullable.error.regmap.Country.regstats.lupdate,regstat.lupdate.nullable.error.regstats[0].lupdate,regstat.lupdate.nullable.error.regstats.lupdate,regstat.lupdate.nullable.error.lupdate,regstat.lupdate.nullable.error.java.util.Date,regstat.lupdate.nullable.error,regmap.Regstat.lupdate.nullable.regmap.Country.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.regmap.Country.regstats.lupdate,regmap.Regstat.lupdate.nullable.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.regstats.lupdate,regmap.Regstat.lupdate.nullable.lupdate,regmap.Regstat.lupdate.nullable.java.util.Date,regmap.Regstat.lupdate.nullable,regstat.lupdate.nullable.regmap.Country.regstats[0].lupdate,regstat.lupdate.nullable.regmap.Country.regstats.lupdate,regstat.lupdate.nullable.regstats[0].lupdate,regstat.lupdate.nullable.regstats.lupdate,regstat.lupdate.nullable.lupdate,regstat.lupdate.nullable.java.util.Date,regstat.lupdate.nullable,nullable.regmap.Country.regstats[0].lupdate,nullable.regmap.Country.regstats.lupdate,nullable.regstats[0].lupdate,nullable.regstats.lupdate,nullable.lupdate,nullable.java.util.Date,nullable]; arguments [lupdate,class regmap.Regstat]; default message [Property [{0}] of class [{1}] cannot be null]
Around line 66 of grails-app/controllers/regmap/CountryuploadController.groovy
63:                     render ("To be more precise: ${exturl} ${impdate} ${lupdate} ${impnote} ${ref} ${status} <BR>")
64:                     def qp = Country.findByCountryname(cont)
65:                         qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, lupdate:lupdate,impnote:impnote,reg:ref,status:status))
66:                         qp.save(flush:true,failOnError:true)

UPDATE: For the fun of it, I set both dates to nullable as well, then I got potentially the least helpfull error message ever URI

/RegMap/countryupload/loadcsv
Class
java.lang.NullPointerException
Message
null

And I did exactly the same check against the database earlier up to check that cont existed.

EDIT 2 Changed the render line so people can see what is in each variable: Got this message (when both impdate, impnote, exturl and lupdate is nullable)

Writing some stuff
To be more precise: exturl:N/A impdate:2014-06-30 update:2014-09-29 impdate: ref:FATCA status:3 
Error 500: Internal Server Error
URI
/RegMap/countryupload/loadcsv
Class
java.lang.NullPointerException
Message
null 

Upvotes: 0

Views: 386

Answers (1)

sebnukem
sebnukem

Reputation: 8333

Check that the parent instance has been found before adding the child record:

qp = Country.findByCountryname(cont)
if (qp) {
    qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, update:lupdate, impnote:impnote, reg:ref, status:status)).save(flush:true,failOnError:true)
}

Upvotes: 1

Related Questions