animalfindingstuff
animalfindingstuff

Reputation: 73

String size in Grails too big for database field

I have Message domain class that is used to send an email to site administrators

class Message {

  String name
  String subject
  String body

}

I thought String would not have a maximum size but when people put in messages that are too big then I see exceptions in my log files. For now I put a constraint in to limit the size of the message to 250 but cannot make it bigger or else the save fails. I am using PostgreSQL 9.1.1 and Grails 1.3.7. Does anyone know a way to fix this?

Upvotes: 7

Views: 5958

Answers (4)

Alexander Suraphel
Alexander Suraphel

Reputation: 10613

Setting type in constraints block doesn't work!

Put in the mapping block instead.

static mapping = {
   body type: 'text'
}

Upvotes: 1

Ankit Kumar Singh
Ankit Kumar Singh

Reputation: 557

The max size of a VARCHAR is set in the database, Grails doesn't have any say in the matter. What you can do is tell Grails what you need, and it will generate DDL that uses a clob instead of a string.

Change the mapping for the column to use type: "text", while adding the maxSize you want in your constraints. Grails will figure out from this that it should set the column data type to clob.

(Generally a clob field will hold up to 4GB, though some databases allow changing that.)

Upvotes: 0

Chris
Chris

Reputation: 8109

Add this to your domain class:

static constraints = {
    body(maxSize:1000)
}

Upvotes: 7

chrislatimer
chrislatimer

Reputation: 3560

You can specify the data type using a constraint:

static constraints = {
  body type:'text'
}

Upvotes: 8

Related Questions