MongoEngine / MongoEngine/mongoengine

On update the model is not validated

Open
#1,287 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Hi

For the following document model:

class Inventory(Document):
    store_id = StringField(required=True)
    product_sku = StringField(unique_with=['store_id'], required=True)
    is_product_enabled = BooleanField(default=False)
    stock = IntField(required=True, min_value=0, default=0)
    created_time = DateTimeField(default=datetime.now)
    updated_time = DateTimeField(default=datetime.now)

When running the following query to decrease the stock:

inventory_obj = Inventory.objects.get(product_sku=product_sku, store_id=store_id)
inventory_obj.update(dec__stock=10)

Document before the update:

{
    "_id" : ObjectId("57273c29aafbb3d5bf7b99f9"),
    "store_id" : "1",
    "product_sku" : "1",
    "is_product_enabled" : true,
    "ready_stock" : 7,
    "stock" : 6,
    "created_time" : ISODate("2016-05-02T17:08:17.452Z"),
    "updated_time" : ISODate("2016-05-02T17:26:39.756Z")
}

Post the update

{
    "_id" : ObjectId("57273c29aafbb3d5bf7b99f9"),
    "store_id" : "1",
    "product_sku" : "1",
    "is_product_enabled" : true,
    "stock" : -4,
    "created_time" : ISODate("2016-05-02T17:08:17.452Z"),
    "updated_time" : ISODate("2016-05-02T18:27:58.592Z")
}

The stock becomes negative. Is this the desired behaviour?
Is there a work around for validating the min_value?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Inventory.objects.get(...).update(dec__stock=10) entry point and trace how min_value validation is handled during atomic updates. Look for existing update-validation tests or documentation; done means the expected behavior for negative stock and any supported workaround are established with a reproducible test or clear documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.