Creating an Optional One-to-One Relationship that's unqiue
-
03-07-2021 - |
سؤال
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 {
}
المحلول
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
لا تنتمي إلى StackOverflow