varchar 列的 Grails GORM 映射约束
Grails GORM mapping contraints for varchar columns
我正在审查与我服务于同一项目的前员工编写的代码。在审查他们的代码时,我遇到了许多 Domain
class 类似的约束:
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
.....
}
我明白 null
值和空字符串是不同的,因此存在 nullable
和 blank
约束。但是你真的应该将 0
指定为 nullable
列的最小长度并为那些 non-nullable
列指定 1
吗?
如果确实如此,那么与那些不使用类似约束的 Domains
有什么区别?在阅读他们的代码之前,我已经编写了很多 Domain
classes 只是使用 only nullable
约束,他们工作得很好.
我认为约束上存在冗余。
我会把它重构成这样的:
static constraints = {
.....
title nullable: false, max:50
notes nullable: true, max:500
.....
}
我正在审查与我服务于同一项目的前员工编写的代码。在审查他们的代码时,我遇到了许多 Domain
class 类似的约束:
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
.....
}
我明白 null
值和空字符串是不同的,因此存在 nullable
和 blank
约束。但是你真的应该将 0
指定为 nullable
列的最小长度并为那些 non-nullable
列指定 1
吗?
如果确实如此,那么与那些不使用类似约束的 Domains
有什么区别?在阅读他们的代码之前,我已经编写了很多 Domain
classes 只是使用 only nullable
约束,他们工作得很好.
我认为约束上存在冗余。
我会把它重构成这样的:
static constraints = {
.....
title nullable: false, max:50
notes nullable: true, max:500
.....
}