mongoengine
mongoengine copied to clipboard
IntField with default value of 0 is lost on subsequent saves
Similar to BooleanFields with default values of False (see issue #282), IntFields with default values of 0 are dropped from the database when the originally created document is tweaked and saved.
Test code:
from mongoengine import *
connect("wolf")
class Doc(Document):
count = IntField(default=0)
name = StringField()
doc = Doc()
doc._get_collection().drop()
doc.save()
for d in doc._get_collection().find():
print d
# Uncomment this line, and things work fine
# doc.reload()
doc.name = "foo"
doc.save()
for d in doc._get_collection().find():
print d
This has output like this:
{u'count': 0, u'_types': [u'Doc'], u'_id': ObjectId('4f2223111725c673f6000000'), u'_cls': u'Doc'}
{u'_types': [u'Doc'], u'_id': ObjectId('4f2223111725c673f6000000'), u'_cls': u'Doc', u'name': u'foo'}
Note that count
is missing from the latter.
If you uncomment the doc.reload()
call, or query for the document any other way, things work as expected.
Seems like the fix for issue #282 in commit 6419a8d should be checking for not-None instead of truthiness?
Ie, instead of:
if value or isinstance(value, bool): continue
it should be:
if value is not None: continue
Common! Seriously? Any updates on this issue?
I'm also running into this issue. However, this is a fork of the mongoengine repository. The correct place for reporting this issue is https://github.com/MongoEngine/mongoengine/issues?state=open
It would be awesome if you can report the issue there with your commit.
@kristinn done.
Great. :+1:
I didn't want to open it there since it's your commit and I didn't want to create confusion.
Thanks. :smiley_cat: