Grails save(flush:true) not immediately persisting to database

863 views
Skip to first unread message

Vishesh Singhal

unread,
May 23, 2015, 8:54:26 AM5/23/15
to grails-de...@googlegroups.com
I am using following code - 

   
Topic topic = new Topic()
    topic
.name = "y u no save"
    topic
.save(flush:true, failOnError:true)
   
   
def promiseList = new PromiseList()
    subtopicNames
.each { sName ->
      promiseList
<< Topic.async.task {
           
Subtopic subtopic = new Subtopic()
           subtopic
.name = sName
           
/* ... long running tasks like web service call ... */
           log
.info("Topic Id : $topic.id")
           subtopic
.topic = topic
           subtopic
.save(flush: true, failOnError: true)
     
}
   
}


   
def subtopics = promiseList.get()


I get this error-

 
   Detail: Key (topic_id)=(517333) is not present in table "topic".; nested exception is org.postgresql.util.PSQLException: ERROR: insert or update on table "subtopic" violates foreign key constraint "fkfd7d3e7098cf2d58"
     
Detail: Key (topic_id)=(517333) is not present in table "topic".


When I check database for topic with id 517333, it actually doesn't exist, while the log in `aync.task` block prints 'Topic Id : 517333'. What is going on here, how to force the topic save when required.

   
 Topic -
   
   
class Topic {
     
String name
     
static hasMany = [subtopics: Subtopic]
   
}
   
   
Subtopic -
   
   
class Subtopic {
     
String name
     
static belongsTo = [topic: Topic]
   
}

Graeme Rocher

unread,
May 25, 2015, 2:52:44 AM5/25/15
to grails-de...@googlegroups.com
You can force an insert by using insert() instead of save(), depending
on the schema this may be necessary as Hibernate sometimes cannot know
if it is an update or an insert and guesses wrong
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/d9b5ed23-f5e3-486c-88b1-8820caae1e7a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Graeme Rocher

Vishesh Singhal

unread,
May 25, 2015, 3:22:12 AM5/25/15
to grails-de...@googlegroups.com
replacing save with insert also gives the same error. The exception is also same - 

Hibernate operation: could not execute statement; SQL [n/a]; ERROR: insert or update on table "sub_topic" violates foreign key constraint "fkfd7d3e7098cf2d58"


Looks like the underline sql query is not affected even on calling insert.

Graeme Rocher

unread,
May 25, 2015, 5:20:52 AM5/25/15
to grails-de...@googlegroups.com
In that case the error seems correct, you are trying to insert data
that violates a foreign key constraint
>> > email to grails-dev-disc...@googlegroups.com.
>> > To post to this group, send email to grails-de...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/grails-dev-discuss/d9b5ed23-f5e3-486c-88b1-8820caae1e7a%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Graeme Rocher
>
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/c8430a01-9805-4a8f-a935-51ac9fd4e33a%40googlegroups.com.

Esau Rodriguez

unread,
May 25, 2015, 5:25:05 AM5/25/15
to grails-de...@googlegroups.com
Hi Vishesh,
which save did you replace? The one inserting topic or the one
inserting sub-topic?

Regards,
Esau Rodriguez.
>> > email to grails-dev-disc...@googlegroups.com.
>> > To post to this group, send email to grails-de...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/grails-dev-discuss/d9b5ed23-f5e3-486c-88b1-8820caae1e7a%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Graeme Rocher
>
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/c8430a01-9805-4a8f-a935-51ac9fd4e33a%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Esaú Rodríguez
esa...@gmail.com

Vishesh Singhal

unread,
May 25, 2015, 5:36:01 AM5/25/15
to grails-de...@googlegroups.com
both
>> > email to grails-dev-discuss+unsub...@googlegroups.com.
>> > To post to this group, send email to grails-de...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/grails-dev-discuss/d9b5ed23-f5e3-486c-88b1-8820caae1e7a%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Graeme Rocher
>
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages