Skip to content

Commit

Permalink
fix(Django): fix daily import script (avoid ON CONFLICT DO UPDATE err…
Browse files Browse the repository at this point in the history
…or. duplicate). ref #376
  • Loading branch information
raphodn committed Sep 15, 2024
1 parent 0656431 commit 549f059
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion open_prices/common/openfoodfacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def import_product_db(flavor: Flavor = Flavor.off, batch_size: int = 1000) -> No
from open_prices.products.models import Product

print((f"Launching import_product_db ({flavor})"))
existing_product_codes = set(Product.objects.values_list("code", flat=True))
existing_product_flavor_codes = set(
Product.objects.filter(source=flavor).values_list("code", flat=True)
)
Expand Down Expand Up @@ -200,9 +201,10 @@ def import_product_db(flavor: Flavor = Flavor.off, batch_size: int = 1000) -> No
product_dict = normalize_product_fields(product_dict)

# Case 1: new OFF product (not in OP database)
if product_code not in existing_product_flavor_codes:
if product_code not in existing_product_codes:
product_dict["code"] = product_code
products_to_create_or_update.append(Product(**product_dict))
existing_product_codes.add(product_code)
added_count += 1

# Case 2: existing product (already in OP database)
Expand Down

0 comments on commit 549f059

Please sign in to comment.