Improve FoodItem uniqueness and add name constraint

Refactored add_meal routes to check for existing FoodItems by name or barcode and improved form handling. Made barcode optional in FoodItemForm. Added a unique constraint on (name, owner_id) for FoodItem in both the model and database migrations, while retaining the (barcode, owner_id) constraint. Updated FoodItem relationship to cascade deletes.
This commit is contained in:
2025-08-11 14:32:53 +02:00
parent ad7f787ce5
commit 7fe30bfebf
5 changed files with 134 additions and 46 deletions

View File

@@ -52,10 +52,16 @@ class FoodItem(db.Model):
fat_100 = db.Column(db.Float, nullable=False)
saturated_fat_100 = db.Column(db.Float)
food_logs = db.relationship("FoodLog", backref="food_item", lazy="dynamic")
food_logs = db.relationship(
"FoodLog",
backref="food_item",
lazy="dynamic",
cascade="all, delete-orphan",
)
__table_args__ = (
db.UniqueConstraint("barcode", "owner_id", name="barcode_owner_key"),
db.UniqueConstraint("name", "owner_id", name="name_owner_key"),
)
def __init__(