简体   繁体   English

varchar列的Grails GORM映射约束

[英]Grails GORM mapping contraints for varchar columns

I'm reviewing codes made by previous employees that served the same project as mine. 我正在审查与我的项目相同的以前的员工制定的代码。 While I'm reviewing their code, I encountered many Domain class constraints similar to this: 在查看他们的代码时,遇到了许多类似于以下内容的Domain类约束:

String title
String notes

static mapping = {
    .....
    title column: 'title'
    notes column: 'notes'
    .....
}

static constraints = {
    .....
    title nullable: false, size: 1..50, blank: true
    notes nullable: true, size: 0..500, blank: true
    .....
}

I get the point that null values and empty strings are different, hence the nullable and blank constraints. 我明白了null值和空字符串是不同的,因此nullable blank约束和blank约束。 But should you really specify 0 as the minimum length of nullable columns and specify 1 to those non-nullable columns? 但是,您是否应该真正将0nullablenullable列的最小长度,并为这些non-nullable列指定1

If it is indeed, then what would be the difference to those Domains that doesn't use similar constraints? 如果确实如此,那么与那些不使用类似约束的Domains有什么区别? Before reading their codes, I've already coded many Domain classes just using only the nullable constraint, and they're working just fine. 阅读他们的代码之前,我已经编码多Domain类只是使用nullable约束,以及他们工作得很好。

I think there is redundancy on the constraints. 我认为约束方面存在冗余。

I would have refactored it to something like this: 我本来可以将其重构为如下形式:

static constraints = {
    .....
    title nullable: false, max:50
    notes nullable: true, max:500
    .....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM