couchdbattachmentektorp

How to update couchdb document without losing existing attachment


I want to update a field in couchdb document which already has an attachment. If I update new field with the current revision then the attachment gets deleted. Is there a way so that I update the field without losing the attachment? I am using Ektorp API to update the document.

Regards Sunil.


Solution

  • When you GET a document that has an attachment it will include the attachment's "stub":

    {
      "_id":"attachment_doc",
      "_rev":1589456116,
      "_attachments":
      {
        "foo.txt":
        {
          "stub":true,
          "content_type":"text\/plain",
          "length":29
        }
      }
    }
    

    When you want to update the document (e.g. through PUT) you must include the "stub", otherwise Couch will remove the attachment.

    Edit: I missed that you were using Erktop.

    The simplest way to preserve attachment stubs then should be for your domain classes to extend CouchDbDocument.

    If for some reason you can't do that, you need to ensure the stubs are (de)serialized. You can use the implementation from here.