You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
an attribute's Field.default has been defined, and
an attribute value is not specified in **kwargs
the object is created with this attribute value set to the Field.default.
For ListField attributes, this causes a problem, because list operations such as
append() will modify the Field.default and subsequent object creations will
behave unexpectedly.
I have included a code illustration below.
The obvious fix is to always initialize an object with a COPY of the Field.default.
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
# Setup Flask
app = Flask(__name__)
# Setup MongoAlchemy
app.config.update(MONGOALCHEMY_DATABASE='issue_db')
db = MongoAlchemy(app)
# Define Item Document
class Item(db.Document):
name = db.StringField(default='')
colors = db.ListField(db.StringField(), default=[])
# Clear Item collection
db.session.clear_collection(Item)
# Create Documents
item1 = Item(name='Item One')
db.session.add(item1)
# item1.colors.append() modifies the Field default
item1.colors.append('red')
item1.colors.append('blue')
item2 = Item(name='Item Two')
db.session.add(item2)
db.session.flush()
# item2 is created with the modified Field default
assert item2.colors==[], 'item2.colors='+str(item2.colors)+' instead of []'
If users are looking for a temporary workaround:
item1.colors = item1.colors + ['red'] # this ensures a copy is assigned
item1.colors = item1.colors + ['blue'] # this ensures a copy is assigned
The text was updated successfully, but these errors were encountered:
Hey @lingthio, thanks for reporting. Unfortunately, this is an issue in mongoalchemy itself. We can fix it on flask-mongoalchemy, but ideally this would be a fix upstream.
Thanks for this great package!
On object creation, where:
the object is created with this attribute value set to the Field.default.
For ListField attributes, this causes a problem, because list operations such as
append() will modify the Field.default and subsequent object creations will
behave unexpectedly.
I have included a code illustration below.
The obvious fix is to always initialize an object with a COPY of the Field.default.
If users are looking for a temporary workaround:
The text was updated successfully, but these errors were encountered: