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

Deleting an element from middle of ListField results in an incorrect list. #1318

Closed
rminsk opened this issue Jun 18, 2016 · 4 comments
Closed
Labels

Comments

@rminsk
Copy link

rminsk commented Jun 18, 2016

import mongoengine as me

class TestList(me.Document):
    test_list = me.ListField(me.IntField())

def test():
    me.connect('test_list', host='mongomock://localhost')

    vals = [1, 2, 3, 4]
    test_list = TestList(test_list=vals)
    test_list.save()

    for tl in TestList.objects():
        print tl.to_json()

    del test_list.test_list[1]
    test_list.save()

    for tl in TestList.objects():
        print tl.to_json()

if __name__ == '__main__':
    test()

Running this gives the following result:

$ python test_list.py
{"_id": {"$oid": "576506f653c26b60b8f9d93c"}, "test_list": [1, 2, 3, 4]}
{"_id": {"$oid": "576506f653c26b60b8f9d93c"}, "test_list": [1, 3, 3, 4]}

I would expect the modified list to be [1, 3, 4]. If I completely replace the list (test_list.test_list = [1, 3, 4]) instead of deleting an element it works fine.

@JohnAD
Copy link
Contributor

JohnAD commented Nov 28, 2016

This bug appears to also effecting the subclass EmbeddedDocumentList. I'm doing work-arounds at the moment, but I'd like to get this fixed at some point.

The bug is still in 10.0.6. So, I'm considering tracing down the source of the error and correcting.

But before I get started: Is anyone else working on this? Any conflicts?

(BTW, the actual list item does get deleted in memory. The error appears to be in actually saving the document with the deletion. Just guessing, it is doing an update rather than a deletion on the list item.)

@wojcikstefan
Copy link
Member

This is definitely a bug, good catch! FYI, I noticed that using test_list.update(set__test_list=test_list.test_list) does the right thing as opposed to using test_list.save().

@wojcikstefan
Copy link
Member

@JohnAD AFAIK nobody else is working on it. I'd love to see a PR if you can find and fix the issue!

@JohnAD
Copy link
Contributor

JohnAD commented Dec 12, 2016

Starting to work on this. But in the meantime, there appears to be an easy workaround; at least for my combination of versions: use the .pop method instead. pop returns a value, but you can simply ignore it.

So, given the example above, replace:

del test_list.test_list[1]

with:

test_list.test_list.pop(1)

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

4 participants