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 6091251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:18:23+00:00 2026-05-23T12:18:23+00:00

Using a Groovy script with grails and want to do an update to a

  • 0

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
}
}
  • 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-23T12:18:23+00:00Added an answer on May 23, 2026 at 12:18 pm

    Instead of doing a new Recorders(input), you probably ought to get it:

    def input = request.JSON
    def instance = Recorders.get(input.getAt('id'))
    instance.properties = input
    instance.save()
    

    Edit

    (From your comment) If it doesn’t exist and you want to insert it:

    def input = request.JSON
    def id = input.getAt('id')
    def instance = Recorders.get(id)
    if(!instance) {
        instance = new Recorders(id: id)
    }
    instance.properties = input
    instance.save()
    

    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: id from the Recorders() constructor.

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

Sidebar

Related Questions

I'm using HtmlUnit in a Groovy script to get a csv file from the
From the book Groovy and Grails recipes I'm using the following code snippet: String
Trying to get a simple string replace to work using a Groovy script. Tried
I'm able to run Groovy scripts from Groovy using proc = cmd /c groovy
Let's say I've compiled a Groovy script using Groovyc, which has generated one or
Using Groovy, I'd like to generate a random sequence of characters from a given
I am writing a Groovy script that needs to POST JSON to a URL.
I'm using Grails and want to use groovyws to call an web-service. But my
I'm working on extending a legacy script system using groovy. The source scripts are
Grails have cofig for spring bean called resources.groovy . And as i understand from

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.