Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Duplicate
-
Affects Version/s: 2.0 final
-
Fix Version/s: None
-
Component/s: Persistence
-
Labels:None
Description
given the following domain class
class Book {
String title
String author
static constraints =
}
Grails creates a table with two columns both of type varchar(255) in the database.
if you now try to save a Book with an author of lenght 300, the validate() and save() methods fail as expected:
def book2 = new test.Book(title:"-"*20,author:"#"*300)
assert book2.validate()==false
assert book2.save(flush:true)==null
but if you try to save a Book with a title of lenght 300, the validate() and save() methods act strange and unexpected since grails has created a constraint in the DB (varchar(255)), but didn't use it for the validation:
def book3 = new test.Book(title:"-"*300,author:"#"*20)
//this evaluates to true in Grails 2.0.0
assert book3.validate()==false
//this throws an exception in 2.0.0 (Value too long for column "TITLE VARCHAR(255) NOT NULL")
assert book3.save(flush:true)==null
It would be great if Grails would create an automatic contraint when creating a varchar(255) column for a string.
Workaround is simply to the the maxSize: constraint for all String properties.
PS: the behaviour in Grails 1.3.7 with the standard DB is different - it seems that the save() works - so there seems to be no varchar(255) constraint on the column
you get the same problem with a property of type byte[]