IcedDante
IcedDante

Reputation: 6842

Grails 3: Update domain fails due to Unique constraint violation

I have the following abstract domain class. It's implemented according to the specs of the SpringSecurity UserDetailsService:

abstract class Users {
    transient passwordEncoder
    String username
    String password
    boolean enabled = true

    static constraints = {
        username blank: false, unique: true
        password blank: false
    }

    static mapping = {
        tablePerHierarchy false
        id generator: 'assigned', name: 'username'
        version false
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = passwordEncoder ?
                passwordEncoder.encode(password) : password
    }
}

And here's the domain class that I am using to implement it:

@Resource(uri="/userinfo")
class UserInfo extends Users {
    String name
    String title
    enum Status {
        ACTIVE,
        INACTIVE,
        UNCONFIRMED
    }

    Status status = Status.UNCONFIRMED
    static belongsTo = [organization: Organization]
    static constraints = {
        name blank: false
        organization nullable: false
    }
}

The problem is that I can create instances of UserInfo classes, but I cannot update them. Running the following code:

UserInfo userInfo = UserInfo.findByUsername "[email protected]"
userInfo.status = UserInfo.Status.ACTIVE
if(!userInfo.save()) {
    logger.error "Unable to save user updates"
    userInfo.errors.allErrors.each {
        println it
    }
}

results in the error:

Field error in object 'com.UserInfo' on field 'username': rejected value [[email protected]]; codes [com.UserInfo.username.unique.error.com.UserInfo.username,com.UserInfo.username.unique.error.username,com.UserInfo.username.unique.error.java.lang.String,com.UserInfo.username.unique.error,userInfo.username.unique.error.com.UserInfo.username,userInfo.username.unique.error.username,userInfo.username.unique.error.java.lang.String,userInfo.username.unique.error,com.UserInfo.username.unique.com.UserInfo.username,com.UserInfo.username.unique.username,com.UserInfo.username.unique.java.lang.String,com.UserInfo.username.unique,userInfo.username.unique.com.UserInfo.username,userInfo.username.unique.username,userInfo.username.unique.java.lang.String,userInfo.username.unique,unique.com.UserInfo.username,unique.username,unique.java.lang.String,unique]; arguments [username,class com.UserInfo,[email protected]]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]

What am I doing wrong?

Upvotes: 0

Views: 358

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

What am I doing wrong?

As far as I can tell, nothing. That looks like a bug in the validation support.

Upvotes: 0

Related Questions