Difference between blank and null constraints

我的梦境 提交于 2019-12-10 01:47:43

问题


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

What effect does blank : true have ?


回答1:


  • blank:true means the field accepts an empty string or one composed only by spaces as valid values. Eg: "", " "
  • nullable:true means the field accepts null as valid value

They can be used together. Eg:

title blank:false, nullable: true



回答2:


While the answer by aruizca is correct and descriptive, I found this while reading the book: "Programming Grails" by Burt Beckwith.

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.

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.




回答3:


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

What effect does blank : true have ?

blank: false protects empty values( e.g. "", " ", etc) from being set on that field. This has nothing to do with the field having the constraint NOT NULL on mysql. That happened because Grails constraints default to nullable: false on every field unless you explicitly set it to nullable: true.



来源:https://stackoverflow.com/questions/29203099/difference-between-blank-and-null-constraints

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!