When you update an existing document you must include the document's
current _rev. CouchDB checks the _rev is correct before updating the
database.
In practice, this means you must first get the document from the
database before making changes and saving it. For example, to change a
password you would do:
doc = db.get('a...@atomised.coop')
doc['password'] = '35gst66s'
db.save(doc)
Hope that helps.
- Matt
>
> doc = {'_id' : 'a...@atomised.coop', 'password' : '35gst66s'}
> db.save(doc)
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/almacmillan/lib/python2.6/CouchDB-0.8-py2.6.egg/couchdb/
> client.py", line 407, in save
> File "/home/almacmillan/lib/python2.6/CouchDB-0.8-py2.6.egg/couchdb/
> http.py", line 405, in put_json
> File "/home/almacmillan/lib/python2.6/CouchDB-0.8-py2.6.egg/couchdb/
> http.py", line 384, in put
> File "/home/almacmillan/lib/python2.6/CouchDB-0.8-py2.6.egg/couchdb/
> http.py", line 419, in _request
> File "/home/almacmillan/lib/python2.6/CouchDB-0.8-py2.6.egg/couchdb/
> http.py", line 306, in request
> couchdb.http.ResourceConflict: ('conflict', 'Document update
> conflict.')
>
> Am I getting this wrong or is this a bug?
>
> I am a beginner programmer so this may very well be so!
>
> Any help really appreciated
>
> Kind thanks
> AL
>
> --
> You received this message because you are subscribed to the Google Groups "CouchDB-Python" group.
> To post to this group, send email to couchdb...@googlegroups.com.
> To unsubscribe from this group, send email to couchdb-pytho...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/couchdb-python?hl=en.
>
>
If you use the same _id it should not create a new document. For
instance, using a new, blank database:
>>> len(list(db.view('_all_docs')))
0
>>> db.save({'_id': 'me', 'name': 'Matt'})
('me', '1-47804390369d435cb116403da935bdd5')
>>> len(list(db.view('_all_docs')))
1
>>> me = db.get('me')
>>> me
<Document 'me'@'1-47804390369d435cb116403da935bdd5' {'name': 'Matt'}>
>>> me['name'] = 'Matt Goodall'
>>> db.save(me)
('me', '2-8b7344ea179d5b721ce4546e1a4566a9')
>>> len(list(db.view('_all_docs')))
1
Are you sure you're using the same _id? If you are sure, please could
you post a short example that creates two documents.
- Matt
Sorry for the slow response but I've been busy today and only just got
back in after an evening out with friends.
To append to a list you have two choices:
1. GET the current version of the document, append the new item to the
list, and PUT the document back, e.g.
doc = db.get('mydoc')
doc['tags'].append('newtag')
db.save(doc)
2. Call a document update handler,
http://wiki.apache.org/couchdb/Document_Update_Handlers.
Unfortunately, couchdb-python doesn't have explicit support for update
handlers right now but you can use the Database instance's resource
attribute.
So, if you have the following design document, _design/foo, containing
an update handler, add_tag, that simply appends a tag to a document's
current list of tags:
{
"_id": "_design/foo",
"updates": {
"add_tag": "function(doc, req) {doc.tags.push(req.query.tag);
return [doc, 'ok']}"
}
}
You would call that using:
add_tag = db.resource('_design', 'foo', '_update', 'add_tag')
add_tag.post('mydoc', tag='newtag')
Those should have the same effect (assuming I typed it all in
correctly ;-)) but option 2 has the advantage of not having to fetch
and send the entire document each time.
- Matt