You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to move a document to another database but when I do this twice I get this error:
AssertionError: doc_id 1 already exists
This is probably because I insert the document in the 2th database and then remove it from the first database. This causes that the second document that I insert into database 2 also has document id 1. Is there any workaround for this struggle?
My first python file:
fromtinydbimportTinyDB, Querydb=TinyDB('db1.json')
db2=TinyDB('db2.json')
defmoveDocumentToDb2(id):
db1=TinyDB('db1.json')
db2=TinyDB('db2.json')
q=Query()
document=db.search(q.id==id)[0]
db2.insert(document)
db1.remove(q.id==id)
# Inserted document to the first database and it gets the doc_id 1db.insert({'id': 1, 'type': 'apple', 'count': 7})
# Move the 'apple' document to db2moveDocumentToDb2(1)
My second python file:
fromtinydbimportTinyDB, Querydb=TinyDB('db1.json')
db2=TinyDB('db2.json')
defmoveDocumentToDb2(id):
db1=TinyDB('db1.json')
db2=TinyDB('db2.json')
q=Query()
document=db.search(q.id==id)[0]
db2.insert(document)
db1.remove(q.id==id)
# This document will also get the doc_id 1 (this is the issue)db.insert({'id': 2, 'type': 'peach', 'count': 3})
# Now when I try to add the 'peach' document to db2 I get the error # because they both have the same doc_idmoveDocumentToDb2(2)
The text was updated successfully, but these errors were encountered:
when retrieving a document from the database, it looks like a dict but really it's a wrapper that contains the data as well as the document ID. When inserting this wrapper, TinyDB will find that the document already has an ID and will use it. Which fails as the ID is already used. To work around this, you can convert the document to an actual dict using db1.insert(dict(document)). That way a new ID will be generated.
This error is something that we didn't think about in #303. As we can't change this behavior without breaking backwards compatibility I'll see if I can document this behavior better.
I'm trying to move a document to another database but when I do this twice I get this error:
This is probably because I insert the document in the 2th database and then remove it from the first database. This causes that the second document that I insert into database 2 also has document id 1. Is there any workaround for this struggle?
My first python file:
My second python file:
The text was updated successfully, but these errors were encountered: