lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

Double Insertion of Documents #102

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago

Please see the following Repro and the Output:-

from couchdb import Server

class CreateCouchDBDatabase:

    def __init__(self, database, username, password, hostname, port,
dbname, couchDBName, couchDocName):
        self.database = database
        self.username = username
        self.password = password
        self.hostname = hostname
        self.port = port
        self.dbname = dbname
        self.couchDBName = couchDBName
        self.couchdocName = couchDocName

    def __repr__(self):
        print "Database Params Are"
        print "Database Backend: ", self.database
        print "Database UserName: ", self.username
        print "Database Password: ", self.password
        print "Database HostName: ", self.hostname
        print "Database PortNo: ", self.port
        print "Database DatabaseName: ", self.dbname
        print "Database Couch DatabaseName: ", self.couchDBName
        print "Database Couch Database Doc: ", self.couchdocName

    def createCouchDB(self):
        server = Server('http://127.0.0.1:5984')
        try:
            db = server.create(self.couchDBName)
            return db
        except:
            """
            If Database Server Already Exists then Just return DB instance
            without deleting the database
            """
            print "Database Already Exists"
            return server[self.couchDBName]

    def addDocument(self, db):
        db[self.couchdocName] = {'database':self.database,
'username':self.username, 'password':self.password,
'hostname':self.hostname, 'port':self.port, 'dbname':self.dbname}

if __name__ == "__main__":
    createdb = CreateCouchDBDatabase('mysql', 'root', 'password',
'localhost', 3306, 'pydb', 'urldb', 'urlzeventserver')
    db = createdb.createCouchDB()
    createdb.addDocument(db)

OUTPUT:----

Traceback (most recent call last):
  File "createdb.py", line 51, in <module>
    createdb.addDocument(db)
  File "createdb.py", line 41, in addDocument
    db[self.couchdocName] = {'database':self.database,
'username':self.username, 'password':self.password,
'hostname':self.hostname, 'port':self.port, 'dbname':self.dbname}
  File
"lib/python2.6/site-packages/CouchDB-0.6-py2.6.egg/couchdb/client.py", line
323, in __setitem__
  File
"lib/python2.6/site-packages/CouchDB-0.6-py2.6.egg/couchdb/client.py", line
985, in put
  File
"lib/python2.6/site-packages/CouchDB-0.6-py2.6.egg/couchdb/client.py", line
1027, in _request
couchdb.client.ResourceConflict: ('conflict', 'Document update conflict.')

I read through few of the posts which mentioned that the latest trunk
solves this issue. But this repro still does the same. Please let me know
if there is something I am doing wrong

Original issue reported on code.google.com by tarun.pa...@gmail.com on 1 Dec 2009 at 5:01

GoogleCodeExporter commented 8 years ago
This is almost certainly the same as issue 85. Please can you test with the 
latest 
httplib2 from their source repository.

Original comment by matt.goo...@gmail.com on 1 Dec 2009 at 5:14

GoogleCodeExporter commented 8 years ago
Yes.. I took the hg clone of the latest trunk.. It gave the same error. Could 
you 
please check the repro on your system as well..

Thanks and regards
Tarun Pasrija

Original comment by tarun.pa...@gmail.com on 1 Dec 2009 at 6:17

GoogleCodeExporter commented 8 years ago
Tarun, are you deleting the database before each time you run your script?

Original comment by matt.goo...@gmail.com on 1 Dec 2009 at 6:59

GoogleCodeExporter commented 8 years ago
Hi Matt

This script does the job of creating a database and then putting a document in 
it. So
I am handling both the cases using try, catch if the database already exists. 
But in
both the cases even if the database does not exist it still throws this error. I
would request you to just copy paste this code as is and try this out on 
couchDB. 

Thanks and Regards
Tarun Pasrija

Original comment by tarun.pa...@gmail.com on 2 Dec 2009 at 7:15

GoogleCodeExporter commented 8 years ago
OK, I think this is just a misunderstanding of how CouchDB works.

You cannot modify an existing document without providing the document's current 
_rev. The 2nd time your script runs you're updating an existing document but 
there's no _rev 
at all.

"db[self.couchdocName] = {...}" just results in a HTTP PUT, so see 
http://wiki.apache.org/couchdb/HTTP_Document_API#PUT for more information.

Original comment by matt.goo...@gmail.com on 2 Dec 2009 at 9:17

GoogleCodeExporter commented 8 years ago
Yes, I do understand that I cannot modify. The problem I specified is by 
running the
script just once when the document ID specified does not exist. Would it be 
possible
to try and run the script once and see it. I believe I would then not have 
logical
conclusions as one reason or the other.

Its as simple as testing the sample code in the client.py
for doc in db.update([
        ...     Document(type='Person', name='John Doe'),
        ...     Document(type='Person', name='Mary Jane'),
        ...     Document(type='City', name='Gotham City')
        ... ]):

If you check the CouchDB Admin. There would be 6 entries instead of 3.

Thanks and Regards
Tarun Pasrija

Original comment by tarun.pa...@gmail.com on 2 Dec 2009 at 10:36

GoogleCodeExporter commented 8 years ago
I'm fairly confident that the "6 entries instead of 3" problem is due to the 
known 
httplib2 problem documented in issue 85.

I have run the script and that's when I realised that the database and document 
are not 
necessarily being deleted before each test run. For clarity, here are my test 
findings:

1. httplib2 0.5.0, no database -- ResourceConflict
2. httplib2 from hg, no database -- no error
3. httplib2 from hg, existing database and document -- ResourceConflict

Please could you check again. If you're still having problems then post back 
here with 
the exact script you're using (the one you posted doesn't actually run ;-)) and 
the 
versions of CouchDB, couchdb-python, httplib2 and Python.

Thanks, Matt.

Original comment by matt.goo...@gmail.com on 2 Dec 2009 at 11:23

GoogleCodeExporter commented 8 years ago
Going to close this as Invalid for now, since it's probably not our issue.

Original comment by djc.ochtman on 14 Dec 2009 at 11:07