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
{{ message }}
Repository navigation
dec atomic operation on Float/Decimal/Int Field is broken #2339
It seems that the mongoengine is broken for atomic dec operation a FloatField or DecimalField.
Here is what i am trying -
class Account (DynamicDocument):
user = ReferenceField(User,passthrough=True,reverse_delete_rule=True,required=True,unique=True)
amount = FloatField(min_value=0,required=True)
Now I am trying to perform a atomic "dec" operation. The "amnt" value is passed as 10
Good catch, its taking the min_value into account to validate the value but it shouldn't in case of inc/dec
As a workaround, you can use the __raw__ update, it will bypass the mongoengine validation: Account.objects(user=usr).update(__raw__={"$inc": {"amount": -amnt}})
changed the title [-]dec atomic operation on floating field or decimal field is broken[/-][+]dec atomic operation on Float/Decimal/Int Field is broken[/+]on Jun 20, 2020
Dear all. I'm also experiencing this issue. The thread seems to have been closed but the bug persists in the latest version v0.22.1.
I was wondering if there is any plan on a fix? Thanks in advance.
The issue is that the validation in IntField is incorrect: when validating, the comparison done is the following:
ifself.min_valueisnotNoneandvalue<self.min_value:
self.error("Integer value is too small")
However, here value is the argument of dec, which means that for a min_value=0, this will fail for any decrement.
I believe the problem lies in the fact that this is the wrong validation function to use in this context.
The same issue lies with the validation for the max_value: if someone were to say that max_value=0 for a field containing only non-positive integers, then any inc command will fail.
I do not believe that the validation should be skipped for either inc or dec, but the validation should be done by taking into account the current value of the field, as well as the increment/decrement, although I'm not sure if this is feasible in the current framework (I'm fairly new to mongoengine, so perhaps I'm wrong on this).
Activity
bagerard commentedon Jun 20, 2020
Good catch, its taking the
min_valueinto account to validate the value but it shouldn't in case of inc/decAs a workaround, you can use the
__raw__update, it will bypass the mongoengine validation:Account.objects(user=usr).update(__raw__={"$inc": {"amount": -amnt}})[-]dec atomic operation on floating field or decimal field is broken[/-][+]dec atomic operation on Float/Decimal/Int Field is broken[/+]shalabhsingh commentedon Oct 15, 2020
This still seems to be broken. Just stumbled on the same error myself for
Intfield.Can this be resolved? Thanks
shuheng-liu commentedon Jan 14, 2021
Dear all. I'm also experiencing this issue. The thread seems to have been closed but the bug persists in the latest version
v0.22.1.I was wondering if there is any plan on a fix? Thanks in advance.
kientuong114 commentedon Feb 18, 2021
The issue is that the validation in
IntFieldis incorrect: when validating, the comparison done is the following:However, here
valueis the argument ofdec, which means that for amin_value=0, this will fail for any decrement.I believe the problem lies in the fact that this is the wrong validation function to use in this context.
The same issue lies with the validation for the
max_value: if someone were to say thatmax_value=0for a field containing only non-positive integers, then anyinccommand will fail.I do not believe that the validation should be skipped for either
incordec, but the validation should be done by taking into account the current value of the field, as well as the increment/decrement, although I'm not sure if this is feasible in the current framework (I'm fairly new to mongoengine, so perhaps I'm wrong on this).sriharan16 commentedon Nov 17, 2022
@bagerard your workaround is working well but when can we expect some fix for this? This was raised more than a couple of years back.
Fix inc/dec rejecting deltas outside a field's min_value/max_value