简体   繁体   English

空白和空约束之间的差异

[英]Difference between blank and null constraints

What is the difference between the blank and null constraints ? 空白和空约束之间有什么区别?

I have the following 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
}

and the mysql table created has the notes set to not null even though I specified notes blank : true 并且创建的mysql表的注释设置为非null,即使我指定了notes blank : true

What effect does blank : true have ? 空白有什么影响:真的有吗?

  • blank:true means the field accepts an empty string or one composed only by spaces as valid values. blank:true表示该字段接受空字符串或仅由空格组成的字符串作为有效值。 Eg: "" , " " 例如: """ "
  • nullable:true means the field accepts null as valid value nullable:true表示该字段接受null作为有效值

They can be used together. 它们可以一起使用。 Eg: 例如:

title blank:false, nullable: true

While the answer by aruizca is correct and descriptive, I found this while reading the book: " Programming Grails " by Burt Beckwith. 虽然aruizca的答案是正确和描述性的,但我在读这本书时发现了这一点:Burt Beckwith的“ Programming Grails ”。

Blanks Versus Nulls In many cases, a blank string and null are equivalent—there is no value set. 空白与空白在许多情况下,空字符串和null是等效的 - 没有设置值。 But HTTP submissions from web browser POST requests send blank strings for inputs without a value. 但是,来自Web浏览器POST请求的HTTP提交会为没有值的输入发送空字符串。 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. 非HTTP数据不会出现这种情况,例如来自其他外部客户端(如Web服务)或测试期间,因此将HTTP空间的空白转换为空值将有助于简化验证。 While we're at it, we can also trim extra whitespace from submitted values. 在我们处理它的同时,我们还可以从提交的值中修剪额外的空格。

It may not be relevant to your question. 它可能与您的问题无关。 Aruizca's answer is all you need, but this can be an additional information about Blanks and Nulls. Aruizca的答案就是您所需要的,但这可以是关于Blanks和Nulls的其他信息。

The other answer are all correct, but to address your question: 另一个答案都是正确的,但要解决你的问题:

mysql table created has the notes set to not null even though I specified notes blank : true 创建的mysql表将注释设置为非null,即使我指定了注释空白:true

What effect does blank : true have ? 空白有什么影响:真的有吗?

blank: false protects empty values( eg "", " ", etc) from being set on that field. blank: false保护在该字段上设置的空值(例如“”,“”等)。 This has nothing to do with the field having the constraint NOT NULL on mysql. 这与mysql上具有约束NOT NULL的字段无关。 That happened because Grails constraints default to nullable: false on every field unless you explicitly set it to nullable: true . 之所以发生这种情况,是因为Grails约束默认nullable: false ,除非您明确将其设置为nullable: true

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

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