grails-data-mapping icon indicating copy to clipboard operation
grails-data-mapping copied to clipboard

New Mapping for static compilation doesn't create constraints

Open gmore opened this issue 6 years ago • 2 comments

Using the new mapping Gorm DSL with MappingBuilder, the mapping on the database is correct, but the mapping is not translated in any constraints.

Steps to Reproduce

  1. Create a basic domain
@CompileStatic
class Sample {

    String optional
    String shorttext

    static mapping = orm {
        property "optional", { nullable true }
        property "shorttext", { maxSize 5 }
    }
}
  1. Create a basic unit test like
class SampleSpec extends Specification implements DomainUnitTest<Sample> {

    def setup() {
    }

    def cleanup() {
    }

   void "test optional"() {
        given:
            Sample sample = new Sample()
        when:
            sample.validate()
        then:
            !sample.hasErrors()
    }

    void "test maxSize"() {
        given:
        Sample sample = new Sample()
        sample.optional = "value"
        sample.shorttext = "123456"
        when:
        sample.validate()
        then:
        sample.hasErrors()
    }
}

Expected Behaviour

Both validation tests should pass

Actual Behaviour

Both validation test fails

I expect that mapping definition will imply constraints.

Alternatively constraints definition could follow the MappingBuilder paradigm like

static constraints = orm {
        property "optional", { nullable true }
        property "shorttext", { maxSize 5 }
}

This is very useful in Ide, in particular now that Intellij lint for Grails is broken from more than a year.

Environment Information

  • **Operating System: MacOs 10.14.3
  • **GORM Version: 6.1
  • **Grails Version: 3.3.8
  • **JDK Version: 1.8

gmore avatar Apr 20 '19 01:04 gmore

Also seeing this. The following mapping does establish constraints correctly on the database. But when saving/validating, it throws errors as though every property is using just the default constraints (basically just nullable: false)

static final mapping = orm {
    table name: '`User`'
    property('password', [
        nullable: false,
        password: true,
        column: '`password`',
    ])
    property('username', [
        nullable: false,
        unique: true,
    ])
    property('email', [
        nullable: true,
        email: true,
    ])
}

jamesdh avatar Dec 22 '20 23:12 jamesdh

Just got bit by this again. I should mention that in my previous example, that is in addition to having a constraints block defined as well that declares the properties as nullable. But it seems simply having the mapping defined via the MappingBuilder breaks that.

jamesdh avatar Apr 13 '21 16:04 jamesdh