Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 688171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:12:09+00:00 2026-05-14T02:12:09+00:00

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

  • 0

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 = {
     Email(email:true)
     username(unique:true)

    }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:12:09+00:00Added an answer on May 14, 2026 at 2:12 am

    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 = {
        email(email:true)
        username(validator: {
                  return !User.findByUsernameILike(it)
                })
      }
    }
    

    Hope that helps.

    [Edit]

    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 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any more generic tools that can compile or basically merge multiple PHP
I have been playing with the Ruby library shoes. Basically you can write a
Can we have a SQL query which will basically help in viewing table and
Basically I need a hosted solution, where one can create an account for a
Basically, I would like a brief explanation of how I can access a SQL
Basically, I want to do zipzam&&&?&&&?&&& -> zipzam%26%26%26?&&&?&&&. I can do that without regex
Can you cast a List<int> to List<string> somehow? I know I could loop through
I am using a method that has the following signature: public static bool TryAuthenticate(string
I basically have this web page where you can narrow your search results by
So basically i have a domain object and a generic repository that can do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.