CRITICAL: bulk-add-by-name always fails — source="shopping" violates DB CHECK constraint #89

Closed
opened 2026-04-18 09:01:30 -07:00 by pyr0ball · 0 comments
Owner

Summary

The POST /api/v1/inventory/items/bulk-add-by-name endpoint always fails for all items. The endpoint (and its underlying store call) hardcodes source="shopping" when creating products and inventory items, but the DB CHECK constraint only allows specific source values.

DB Constraints

products.source: CHECK (source IN ('openfoodfacts', 'manual', 'receipt_ocr'))
inventory_items.source: CHECK (source IN ('barcode_scan', 'manual', 'receipt'))

Affected Code

app/api/endpoints/inventory.pybulk_add_items_by_name():

product, _ = await asyncio.to_thread(
    store.get_or_create_product, entry.name, None, source="shopping"  # ← fails
)
item = await asyncio.to_thread(
    store.add_inventory_item,
    product["id"],
    entry.location,
    quantity=entry.quantity,
    unit=entry.unit,
    source="shopping",  # ← also fails
)

Fix

Change source="shopping" to source="manual" in both calls:

product, _ = await asyncio.to_thread(
    store.get_or_create_product, entry.name, None, source="manual"
)
item = await asyncio.to_thread(
    store.add_inventory_item, product["id"], entry.location,
    quantity=entry.quantity, unit=entry.unit, source="manual",
)

Verified

{
  "added": 0,
  "failed": 1,
  "results": [{"name": "E2E Carrots", "ok": false, "error": "CHECK constraint failed: source IN ('openfoodfacts', 'manual', 'receipt_ocr')"}]
}
## Summary The `POST /api/v1/inventory/items/bulk-add-by-name` endpoint always fails for all items. The endpoint (and its underlying store call) hardcodes `source="shopping"` when creating products and inventory items, but the DB CHECK constraint only allows specific source values. ## DB Constraints `products.source`: `CHECK (source IN ('openfoodfacts', 'manual', 'receipt_ocr'))` `inventory_items.source`: `CHECK (source IN ('barcode_scan', 'manual', 'receipt'))` ## Affected Code `app/api/endpoints/inventory.py` — `bulk_add_items_by_name()`: ```python product, _ = await asyncio.to_thread( store.get_or_create_product, entry.name, None, source="shopping" # ← fails ) item = await asyncio.to_thread( store.add_inventory_item, product["id"], entry.location, quantity=entry.quantity, unit=entry.unit, source="shopping", # ← also fails ) ``` ## Fix Change `source="shopping"` to `source="manual"` in both calls: ```python product, _ = await asyncio.to_thread( store.get_or_create_product, entry.name, None, source="manual" ) item = await asyncio.to_thread( store.add_inventory_item, product["id"], entry.location, quantity=entry.quantity, unit=entry.unit, source="manual", ) ``` ## Verified ```json { "added": 0, "failed": 1, "results": [{"name": "E2E Carrots", "ok": false, "error": "CHECK constraint failed: source IN ('openfoodfacts', 'manual', 'receipt_ocr')"}] } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Circuit-Forge/kiwi#89
No description provided.