空白和空约束之间的区别
Difference between blank and null constraints
空白约束和空约束有什么区别?
我有以下class
class Task {
String title
String notes
TekUser assignedTo
Date dueDate
TekEvent event
static constraints = {
title blank:false
notes blank: true , maxSize: 5000
assignedTo nullable:true
dueDate nullable:true
}
static belongsTo = TekEvent
}
并且创建的 mysql table 将注释设置为非空,即使我指定了 notes blank : true
blank : true 有什么作用?
blank:true
表示该字段接受空字符串或仅由空格组成的字符串作为有效值。例如:""
、" "
nullable:true
表示该字段接受 null
作为有效值
它们可以一起使用。例如:
title blank:false, nullable: true
虽然 aruizca 的回答是正确的和描述性的,但我在阅读这本书时发现了这一点:Burt Beckwith 的“Programming Grails”。
Blanks Versus Nulls
In many cases, a blank string and null are
equivalent—there is no value set. But HTTP submissions from web
browser POST requests send blank strings for inputs without a value.
This will not be the case with non-HTTP data, such as from other
external clients like web services or during testing, so converting
blanks to nulls for the HTTP tier will help simplify validation. While
we’re at it, we can also trim extra whitespace from submitted
values.
这可能与您的问题无关。 Aruizca 的答案就是您所需要的,但这可以是有关空白和空值的附加信息。
其他答案都正确,但要解决你的问题:
mysql table created has the notes set to not null even though I
specified notes blank : true
What effect does blank : true have ?
blank: false
防止在该字段上设置空值(例如“”、“”等)。这与在 mysql 上具有约束 NOT NULL
的字段无关。发生这种情况是因为 Grails 将每个字段的 default 约束为 nullable: false
,除非您明确将其设置为 nullable: true
.
空白约束和空约束有什么区别?
我有以下class
class Task {
String title
String notes
TekUser assignedTo
Date dueDate
TekEvent event
static constraints = {
title blank:false
notes blank: true , maxSize: 5000
assignedTo nullable:true
dueDate nullable:true
}
static belongsTo = TekEvent
}
并且创建的 mysql table 将注释设置为非空,即使我指定了 notes blank : true
blank : true 有什么作用?
blank:true
表示该字段接受空字符串或仅由空格组成的字符串作为有效值。例如:""
、" "
nullable:true
表示该字段接受null
作为有效值
它们可以一起使用。例如:
title blank:false, nullable: true
虽然 aruizca 的回答是正确的和描述性的,但我在阅读这本书时发现了这一点:Burt Beckwith 的“Programming Grails”。
Blanks Versus Nulls In many cases, a blank string and null are equivalent—there is no value set. But HTTP submissions from web browser POST requests send blank strings for inputs without a value. This will not be the case with non-HTTP data, such as from other external clients like web services or during testing, so converting blanks to nulls for the HTTP tier will help simplify validation. While we’re at it, we can also trim extra whitespace from submitted values.
这可能与您的问题无关。 Aruizca 的答案就是您所需要的,但这可以是有关空白和空值的附加信息。
其他答案都正确,但要解决你的问题:
mysql table created has the notes set to not null even though I specified notes blank : true
What effect does blank : true have ?
blank: false
防止在该字段上设置空值(例如“”、“”等)。这与在 mysql 上具有约束 NOT NULL
的字段无关。发生这种情况是因为 Grails 将每个字段的 default 约束为 nullable: false
,除非您明确将其设置为 nullable: true
.