Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move a document from a certain database to another one error #354

Open
AlgoQ opened this issue Nov 23, 2020 · 2 comments
Open

Move a document from a certain database to another one error #354

AlgoQ opened this issue Nov 23, 2020 · 2 comments
Labels

Comments

@AlgoQ
Copy link

AlgoQ commented Nov 23, 2020

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:

from tinydb import TinyDB, Query

db = TinyDB('db1.json')
db2 = TinyDB('db2.json')

def moveDocumentToDb2(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 1
db.insert({'id': 1, 'type': 'apple', 'count': 7})

# Move the 'apple' document to db2
moveDocumentToDb2(1)

My second python file:

from tinydb import TinyDB, Query

db = TinyDB('db1.json')
db2 = TinyDB('db2.json')

def moveDocumentToDb2(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_id
moveDocumentToDb2(2)
@msiemens
Copy link
Owner

Hey @JanssensKobe,

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.

@msiemens
Copy link
Owner

Note: I've applied the pinned tag so this issue won't auto-close in order to remind me to update the documentation 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants