mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

IntField with default value of 0 is lost on subsequent saves

Open joeshaw opened this issue 13 years ago • 5 comments

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.

joeshaw avatar Jan 27 '12 04:01 joeshaw

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

joeshaw avatar Jan 27 '12 17:01 joeshaw

Common! Seriously? Any updates on this issue?

yunmanger1 avatar Mar 13 '13 06:03 yunmanger1

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 avatar Apr 04 '13 10:04 kristinn

@kristinn done.

yunmanger1 avatar Apr 04 '13 10:04 yunmanger1

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:

kristinn avatar Apr 04 '13 11:04 kristinn