Amith
Amith

Reputation: 65

grails: Duplicate rows inserted even after unique constraint

I am able to insert multiple rows with same value actionName:"discard" and actionParameter:"". I want to avoid this scenario.

Other constraints are working fine like i am not able to insert actionName:"rate-limit" and actionParameter:"5" twice.

Here is my domain class:

class Exaaction {

String actionName
String actionParameter

static hasMany = [actionFlow:Flow]

static constraints = {
    actionName(blank:false,nullable:false,inList:["discard","rate-limit","redirect"])
    actionParameter(blank:true,nullable:true,unique:'actionName',validator:{val,obj ->
                    if(obj.actionName == 'discard' && val != null){ return false}
                    else if(obj.actionName == 'rate-limit')
                    {
                        Utility util = new Utility()
                        if(!util.validateNum(val,0,2147483647))  {return false}

                    }
                    else if(obj.actionName == 'redirect')
                    {
                        if(val == null) {return false}
                        Utility util = new Utility()
                        if(!(util.validateIp(val) || util.validateIpAndPort(val) || util.validateASAndPort(val)) )  {return false}
                    }
    })

}

}

Please let me know if there is an alternate way to do this? Thanks in advance!!

Upvotes: 0

Views: 190

Answers (2)

Amith
Amith

Reputation: 65

I solved it by using 'null' value as string in the field that is nullable. This is just workaround and not the solution to the problem.

Upvotes: 0

Shashank Agrawal
Shashank Agrawal

Reputation: 25817

This is the implementation by default, i.e. Unique constraint does not work with nullable property. See https://jira.grails.org/browse/GRAILS-10403 issue in reference with https://jira.grails.org/browse/GRAILS-10178 this issue.

Upvotes: 1

Related Questions