histrio/py-couchdb

put_attachment issue

dedalus2000 opened this issue · 1 comments

When I upload one file the code looks like:

doc = db.put_attachment(doc, file_object)

Ok, but now I want to update one field:

doc['onefield'] = 'newcontent'
doc = db.save(doc)

et voilà, the previously uploaded file has been deleted!

This happens because "put_attachment" doesn't update the doc['_attachment'] , so if I save it later I will delete any new attachment.

The problem is that "put_attachment" can't fill the "doc" with all the informations: content_type, revpos, digest, length. In fact couchdb doesn't help us: when we put an attachment it responds only with id, rev and status. Nothing more.

Probably is not a good idea to let put_attachment does the couchdb job (calculating all the file informations).

The only solution seems to be to read the document again from the db.

As far Couchdb as no transaction we cannot write a simple "self.get(id)" after a "resource.put" expecting a good ("congruous") answer: in fact between them someone else can had modified the document.

So I think we have two ways:

  • check revision after the "get". It has to be the same returned by the "put"
  • execute a get(id, rev=returned_revision) so we are sure we are getting the modified we made (if the database has not been compacted between the "put" and the "get")

I prefer the second way