Saving to CouchDB doesn't fetch the document "id" property
Saving a record as a CouchDB document does a HTTP POST request to the database and should read back the resulting JSON message that contains the ID for the newly-generated document. This used to be the case for Lift 2.2, but broke for the 2.3 release. The cause seems to be the DatabaseHelpers.handleUpdateResult method -- it expects a JObject containing JFields of JBool/JString values. Now the \ operator that is used returns the JBool/JString values directly which greatly simplifies the pattern matching code.
Here's the current (broken code):
And here's the working version:
Details, and the whole story on how I stumbled upon this are available in this mailing list thread. Here is a sample project demonstrating the problem and here is a branch that works around it by overriding the JSON parsing mechanism in the same way as described above. I hope this helps.
Here's the current (broken code):
def handleUpdateResult(original: JObject)(json: JValue): Box[JObject] =
for {
obj <- Full(json).asA[JObject] ?~ ("update result is not a JObject: " +
json)
ok <- Full(json \ "ok"
).asA[JField].map(_.value).asA[JBool].filter(_.value) ?~ ("ok not present in
reply or not true: "+json)
id <- Full(json \ "id" ).asA[JField].map(_.value).asA[JString].map(_.s)
?~ ("id not present or not a string: " + json)
rev <- Full(json \ "rev").asA[JField].map(_.value).asA[JString].map(_.s)
?~ ("rev not present or not a string: " + json)
} yield updateIdAndRev(original, id, rev)
And here's the working version:
def handleUpdateResult(original: JObject)(json: JValue): Box[JObject] = {
for {
obj <- Full(json).asA[JObject] ?~ ("update result is not a JObject: " + json)
ok <- Full(json \ "ok" ).asA[JBool].filter(_.value) ?~ ("ok not present in reply or not true: "+json)
id <- Full(json \ "id" ).asA[JString].map(_.s) ?~ ("id not present or not a string: " + json)
rev <- Full(json \ "rev").asA[JString].map(_.s) ?~ ("rev not present or not a string: " + json)
} yield updateIdAndRev(original, id, rev)
}
Details, and the whole story on how I stumbled upon this are available in this mailing list thread. Here is a sample project demonstrating the problem and here is a branch that works around it by overriding the JSON parsing mechanism in the same way as described above. I hope this helps.
Leave a comment
on 2011-11-09 23:04 *
By Indrajit Raychaudhuri
Assigned to set to Indrajit Raychaudhuri
Status changed from Accepted to Fixed
(In revision:43c711edee61f36f592fd585a4945f48a206759f) Saving to CouchDB doesn't fetch the documents 'id' property. Fixes #961.
Thanks to @hdeshev and @marianoguerra for analysis and the suggested fix.
Branch: master
Thanks to @hdeshev and @marianoguerra for analysis and the suggested fix.
Branch: master