Using a Groovy script with grails and want to do an update to a record in the database. I do the basic get the object from JSON and convert it to the Domain class and then do save() on it. From what I understand since I am new to Groovy and grails the save should update if the “id” is already there. But I don’t get that, I get the standard SQL error of “Duplicate entry ‘1’ for key ‘PRIMARY'”. How do I fix this?
def input = request.JSON
def instance = new Recorders(input)
instance.id = input.getAt("id")
instance.save()
and my domain is:
class Recorders {
Integer sdMode
Integer gsmMode
static mapping = {
id generator: "assigned"
}
static constraints = {
sdMode nullable: true
gsmMode nullable: true
}
}
Instead of doing a
new Recorders(input), you probably ought togetit:Edit
(From your comment) If it doesn’t exist and you want to insert it:
I don’t use assigned id generators much, so I’m not sure if Grails will bind the id automatically (since it’s expecting it to be assigned). If it does, you can probably remove the
id: idfrom theRecorders()constructor.