How do I update an instance with a unique field that hasn't changed in Grails?

I am having a problem while trying to update an instance with a constrained field: unique, but has not been modified.

https://grails.github.io/grails-doc/latest/ref/Constraints/unique.html

class SectorEmpresarial{
    Long codigo
    String nombre

    static constraints = {
        nombre  nullable: false, size: 0..50, unique: true
    }
}

      

Example:

  • Created instance

    def sectorEmpresarialInstance = new SectorEmpresarial(codigo:1,nombre:"MY_NAME")
    sectorEmpresarialInstance.save(flush:true)
    
          

  • Editing instance (here ERROR)

    / * params = (codigo: 2, nombre: "MY_NAME"), see that nombre hasn't changed * /

    def sectorEmpresarialInstance = SectorEmpresarial.findByCodigo(1)
    sectorEmpresarialInstance.properties = params 
    sectorEmpresarialInstance.save(flush:true) // Here present error because nombre has constraint: unique.
    
          

+3


source to share





All Articles