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

Please recall fix on: Saving document doesn't create new fields in existing collection #620 #1126

Closed
iici-gli opened this issue Oct 11, 2015 · 1 comment

Comments

@iici-gli
Copy link
Contributor

The fix #620 has a bug. The key for changed_fields should not be field_name. It should use db_field.
When a ListField is missed in database and the field has a db_field is different than the field name, the default value check can put wrong field back to database. Then the database will no longer work.
It is better to take out whole block starting on 'elif field.default:', at line 713. Mongoengine should not change database silently.
The bug is in this code:
elif not only_fields or field_name in only_fields:
changed_fields.append(field_name)

test:

In old time you can have this:

class User(Document):
username = StringField(unique=True, db_field='unm')
password = StringField(db_field='pss')

supper_name = User(username='admin', password='admin')
supper_name.save()

Then you may add some communication information to it:

class User(Document):
username = StringField(unique=True, db_field='unm')
password = StringField(db_field='pss')
communication_information_list = ListField(StringField(), db_field='com_ls')

new_user = User(username='a_user', password='auser', communication_information_list=
['phone: 234 765 1324', 'email: [email protected]'])
new_user.save()

load supper_user back

su = User.objects(username='admin').first()
print su._change_fields

You will see 'communication_information_list ' in changed list instead of 'com_ls'

change the password, then save, in database 'communication_information_list ' : null will be inserted

su.password = 'root'
su.save()

load it back, it will crash

su = User.objects(username='admin').first()

iici-gli added a commit to iici-gli/mongoengine that referenced this issue Oct 13, 2015
…changing database

This resolved issue Please recall fix on: Saving document doesn't create new fields in existing collection MongoEngine#620 MongoEngine#1126
iici-gli added a commit to iici-gli/mongoengine that referenced this issue Oct 18, 2015
@bagerard
Copy link
Collaborator

closing this, tried the snippet and Issue seemed to have been fixed

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

No branches or pull requests

3 participants