Re: couchdb.mapping.Document rev attribute

42 views
Skip to first unread message

Alexander Shorin

unread,
Jul 17, 2012, 10:48:59 AM7/17/12
to couchdb...@googlegroups.com
Hi Adam!

Mostly because this is system internal field, that not supposed to be
touched by clients. Accidental his changing may create unexpectable
conflict situations.
To create mapping.Document instance based on view result you have two ways:
1. Use ViewField to gain result data and Document class as result row wrapper.
2. Wrap view result manually by Document class on instance
initialization. Document revision would be automatically set by `_rev`
key parameter.

--
,,,^..^,,,


On Tue, Jul 17, 2012 at 5:51 PM, adam.cajf <adam...@gmail.com> wrote:
> I'd like to know why rev property of the couchdb.mapping.Document class is
> read-only attribute. Even id attribute can be set, and I need in my project
> to create a new Document with rev, which I obtain from CouchDB view. I know
> that rev is generated in CouchDB, but that's a pity that creators didn't
> predict such an exception. Moreover, in my humble opinion there should be a
> get method for the Document class that could obtain many ids as a paramter.
>
> --
> You received this message because you are subscribed to the Google Groups
> "CouchDB-Python" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/couchdb-python/-/t0XgDS2BQBkJ.
> 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.

adam.cajf

unread,
Jul 18, 2012, 12:09:39 PM7/18/12
to couchdb...@googlegroups.com
Hi Kxepal,

Thank you for your answer. 

I have another case...
There is REST API. Let's assume that user send a document to be updated with the rev parameter. Then if it was possible to set rev in Document and there was a conflict I could receive from couchdb the exception:

HTTP/1.1 409 Conflict
Server: CouchDB/1.2.0 (Erlang OTP/R15B)
Date: Wed, 18 Jul 2012 15:48:21 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 58
Cache-Control: must-revalidate

{"error":"conflict","reason":"Document update conflict."}

and I would like to have access to this exception and not checking manually if rev in the document in couchdb is equal to the rev given by user.

On Tuesday, July 17, 2012 4:48:59 PM UTC+2, Kxepal wrote:
Hi Adam!

Mostly because this is system internal field, that not supposed to be
touched by clients. Accidental his changing may create unexpectable
conflict situations.
To create mapping.Document instance based on view result you have two ways:
1. Use ViewField to gain result data and Document class as result row wrapper.
2. Wrap view result manually by Document class on instance
initialization. Document revision would be automatically set by `_rev`
key parameter.

--
,,,^..^,,,


On Tue, Jul 17, 2012 at 5:51 PM, adam.cajf <adam...@gmail.com> wrote:
> I'd like to know why rev property of the couchdb.mapping.Document class is
> read-only attribute. Even id attribute can be set, and I need in my project
> to create a new Document with rev, which I obtain from CouchDB view. I know
> that rev is generated in CouchDB, but that's a pity that creators didn't
> predict such an exception. Moreover, in my humble opinion there should be a
> get method for the Document class that could obtain many ids as a paramter.
>
> --
> You received this message because you are subscribed to the Google Groups
> "CouchDB-Python" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/couchdb-python/-/t0XgDS2BQBkJ.
> To post to this group, send email to couchdb-python@googlegroups.com.
> To unsubscribe from this group, send email to

Alexander Shorin

unread,
Jul 21, 2012, 12:35:34 AM7/21/12
to couchdb...@googlegroups.com
On Wed, Jul 18, 2012 at 8:09 PM, adam.cajf <adam...@gmail.com> wrote:
> I have another case...
> There is REST API. Let's assume that user send a document to be updated with
> the rev parameter. Then if it was possible to set rev in Document and there
> was a conflict I could receive from couchdb the exception

The base logic is that on conflict situation you need to fetch latest
version and update his data from outdated one if you'd like to force
override data. However, you're still able to set revision by accessing
to Mapping internals:

doc._data['_rev'] = newrev

This is not very correct, because for most conflicts better to analyze
both documents and decide how to merge their data, but if you're sure
and really want just to override new document version by old one you'd
need to explicitly make a hack to note that behavior for others -
sounds quite fair.

--
,,,^..^,,,
>> > 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "CouchDB-Python" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/couchdb-python/-/qNflbpV2c3kJ.
>
> To post to this group, send email to couchdb...@googlegroups.com.
> To unsubscribe from this group, send email to
> couchdb-pytho...@googlegroups.com.

Khanh Tran

unread,
Dec 22, 2013, 4:49:01 PM12/22/13
to couchdb...@googlegroups.com
Can show me a snippet how to do it the second way, "Wrap view result manually by Document class on instance initialization" ?

Alexander Shorin

unread,
Dec 22, 2013, 9:07:47 PM12/22/13
to couchdb...@googlegroups.com
On Mon, Dec 23, 2013 at 1:49 AM, Khanh Tran <khanh...@gmail.com> wrote:
> Can show me a snippet how to do it the second way, "Wrap view result
> manually by Document class on instance initialization

Well, something like:

import couchdb
from couchdb.mapping import Document

server = couchdb.Server()
db = server['somedb']

docs = [
Document.wrap(row.doc)
for row in db.view('_all_docs', include_docs=True)
]

--
,,,^..^,,,
Reply all
Reply to author
Forward
0 new messages