couchdb.http.ResourceConflict: ('conflict', 'Document update conflict.')

1,652 views
Skip to first unread message

handloomweaver

unread,
Oct 13, 2010, 12:23:51 PM10/13/10
to CouchDB-Python
Hi

I would appreciate some advice. I'm trying to simply update an
existing document. I thought from reading the docs it was the same
process as creating a new document. I've made an email address my doc
id in this instance

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

Matt Goodall

unread,
Oct 13, 2010, 12:36:00 PM10/13/10
to couchdb...@googlegroups.com
On 13 October 2010 17:23, handloomweaver <a.w.ma...@gmail.com> wrote:
> Hi
>
> I would appreciate some advice. I'm trying to simply update an
> existing document. I thought from reading the docs it was the same
> process as creating a new document. I've made an email address my doc
> id in this instance

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.
>
>

handloomweaver

unread,
Oct 13, 2010, 12:58:19 PM10/13/10
to CouchDB-Python
Hi

Now that works but it seems to create a new document and not update
the existing? Is that wrong or is that how couchdb works?

AL
On Oct 13, 5:36 pm, Matt Goodall <matt.good...@gmail.com> wrote:
> On 13 October 2010 17:23, handloomweaver <a.w.macmil...@gmail.com> wrote:
>
> > Hi
>
> > I would appreciate some advice. I'm trying to simply update an
> > existing document. I thought from reading the docs it was the same
> > process as creating a new document. I've made an email address my doc
> > id in this instance
>
> 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('...@atomised.coop')
>   doc['password'] = '35gst66s'
>   db.save(doc)
>
> Hope that helps.
>
> - Matt
>
>
>
>
>
> > doc = {'_id' : '...@atomised.coop',  'password' : '35gst66s'}

Matt Goodall

unread,
Oct 13, 2010, 3:23:11 PM10/13/10
to couchdb...@googlegroups.com
On 13 October 2010 17:58, handloomweaver <a.w.ma...@gmail.com> wrote:
> Hi
>
> Now that works but it seems to create a new document and not update
> the existing? Is that wrong or is that how couchdb works?

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

handloomweaver

unread,
Oct 20, 2010, 12:31:07 PM10/20/10
to CouchDB-Python
That was really helpful and did indeed work. Now I want to save to
couchdb fields that contain multiple values that are appended over
time. So in the same vein. How would I save to a ListField to append
to the list and not overwrite what's there?


On Oct 13, 8:23 pm, Matt Goodall <matt.good...@gmail.com> wrote:

Matt Goodall

unread,
Oct 21, 2010, 7:48:30 PM10/21/10
to couchdb...@googlegroups.com
On 20 October 2010 17:31, handloomweaver <a.w.ma...@gmail.com> wrote:
> That was really helpful and did indeed work. Now I want to save to
> couchdb fields that contain multiple values that are appended over
> time. So in the same vein. How would I save to a ListField to append
> to the list and not overwrite what's there?

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

Reply all
Reply to author
Forward
0 new messages