Take the 2-minute tour ×

How can I basically carry out a unique constraint on a string data-type field.

class User{
  String username
  String Email

  static hasMany = [roles:Roles]

     static constraints = {


Is there any simple way to implement username(unique: true)

Or must I manually check the database using methods like .findByNameLike?

The username should be unique, but the uniqueness is should be case-insensitive.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

So, if you want to have unique and case insensitive usernames, there are two possible approaches.

The simple one:

  • Store them upper or lower case and use the unique constraint.

or, regarding performance, more expensive:

  • Store them in mixed case and use a custom validator, which checks the database by comparing given and existing usernames case insensitive.

Now it depends, if you just want to give the user the freedom to enter his username in the case he wants (first possibility) or you want to keep the usernames case for displaying reasons (second possibility).

Your question sounds like the second one, so a custom validator would look like this:

class User { 
  String username 
  String email

  static hasMany = [roles:Roles]
  static constraints = {
    username(validator: {
              return !User.findByUsernameILike(it)

Hope that helps.


As Heinrich states in his comment, the validator above will cause problems when users are able to change their username.

Quick & dirty, but I think this solves the issue:

username(validator: { val, obj ->
                      def similarUser = User.findByUsernameILike(val) 
                      return !similarUser || obj.id == similarUser.id

Beware, it's not tested and I'm not sure if you're able to define variables in validators.

Meta: I would never let the users change their username ;)

share|improve this answer
Good answer! I think you custom validator might have problems when "username" is updated. See n4.nabble.com/Case-Insensitive-Constraint-td1359204.html. Not sure though... –  Heinrich Filter Mar 29 '10 at 13:14
Omg! yes @Heinrich you are right it does fail when I try to update the same field. –  WaZ Mar 29 '10 at 13:41
username(validator: { val, obj -> def similarUser = User.findByUsernameILike(val) return !similarUser || (obj.id == similarUser.id) }) Whats the purpose of obj.id && in the condition. I removed it and it still works fine. –  WaZ Mar 29 '10 at 14:06
That only checks if the instance has an id ... but you're right, obj.id == similarUser.id is enough. Edited, I'm not that good in writing rock-solid code from scratch without testing :) –  air_blob Mar 29 '10 at 14:14
Btw. consider writing unit tests, especially for validators like this, Grails provides awsome possibilities for TDD. –  air_blob Mar 29 '10 at 14:19
show 5 more comments

username(unique:true) is a valid constraint.

To make the constraint case insensitive you would need to write a custom validator. See this discussion thread for more information.

share|improve this answer
Mate, when I implement it I get the following error: No such property: id for class: com.myCompany.User –  WaZ Mar 29 '10 at 13:46
add comment

In order to add another solution of the "quick and dirty" solution of @air_blob, did you try that ?

username(validator: { val, obj ->
                      return !User.findByUsernameIlikeAndIdNotEqual(val, obj.id)                      
share|improve this answer
add comment

Your Answer


