Question

I'm trying to create a simple one to one relationship, but leave it optional. When I attempt to save the "Face" object, I get the "must be unique" validation error. When the user adds a new "Face", a "Nose" isn't required. Later in the process, if the user does add a "Nose", then it needs to be unique.

class Face {
    Nose nose
    static constraints = {
        nose unique: true, nullable: true
    }
}

class Nose {
}
Was it helpful?

Solution

Just as tim_yates mentioned, it does work on Grails 2.1.1. There was a configuration issue on my application.

To test this, I edited the FaceTests.groovy file with this simple test:

package demo

import grails.test.mixin.*
import org.junit.*

@TestFor(Face)
class FaceTests {

    void testSaving() {

       def face = new Face()
        assertNotNull face.save(flush:true)

    }
}

The result:

grails> test-app
| Completed 2 unit tests, 0 failed in 451ms
| Tests PASSED - view reports in /Users/devin/Desktop/grails_demo_app/target/test-reports
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top