Change barcode field from integer to string

Updated the barcode field in FoodItem model and form from integer to string to support barcodes with leading zeros or non-numeric values. Added validation to ensure barcode contains only digits. Updated seed data to use string barcodes.
This commit is contained in:
2025-07-08 11:06:03 +02:00
parent 5b378cad38
commit bb5b2e8bc7
3 changed files with 6 additions and 4 deletions

View File

@@ -41,7 +41,7 @@ class Unit(db.Model):
class FoodItem(db.Model):
__tablename__ = "food_item"
id = db.Column(db.Integer, primary_key=True)
barcode = db.Column(db.Integer)
barcode = db.Column(db.String)
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
name = db.Column(db.String(150), nullable=False)
@@ -66,10 +66,12 @@ class FoodItem(db.Model):
protein: float,
carbs: float,
fat: float,
barcode: Optional[int] = None,
barcode: Optional[str] = None,
sugar: Optional[float] = None,
saturated_fat: Optional[float] = None,
):
if barcode and not barcode.isdigit():
raise ValueError("Barcode must contain only digits.")
self.name = name
self.owner_id = owner_id
self.energy_100 = energy