fixing the db migrations

This commit is contained in:
2025-10-01 12:40:58 -07:00
parent 63b3575797
commit 7ffc57a7a8
7 changed files with 158 additions and 51 deletions

View File

@@ -713,19 +713,22 @@ async def tracker_add_food(data: dict = Body(...), db: Session = Depends(get_db)
db.commit()
db.refresh(tracked_day)
# The quantity is already in grams, so no conversion needed
quantity = grams
# Create a new Meal for this single food entry
# This allows it to be treated like any other meal in the tracker view
# Convert grams to a quantity multiplier based on serving size
food_item = db.query(Food).filter(Food.id == food_id).first()
if not food_item:
return {"status": "error", "message": "Food not found"}
if food_item.serving_size > 0:
quantity = grams / food_item.serving_size
else:
quantity = 1.0 # Default to 1 serving if serving size is not set
# Create a new Meal for this single food entry
# This allows it to be treated like any other meal in the tracker view
new_meal = Meal(name=food_item.name, meal_type="single_food", meal_time=meal_time)
db.add(new_meal)
db.flush() # Flush to get the new meal ID
# Link the food to the new meal
meal_food = MealFood(meal_id=new_meal.id, food_id=food_id, quantity=quantity)
db.add(meal_food)

View File

@@ -14,7 +14,7 @@ import os
# Database setup - Use SQLite for easier setup
# Use environment variables if set, otherwise use defaults
# Use current directory for database
DATABASE_PATH = os.getenv('DATABASE_PATH', '.')
DATABASE_PATH = os.getenv('DATABASE_PATH', '/app/data')
DATABASE_URL = os.getenv('DATABASE_URL', f'sqlite:///{DATABASE_PATH}/meal_planner.db')
# For production, use PostgreSQL: DATABASE_URL = "postgresql://username:password@localhost/meal_planner"
@@ -29,7 +29,7 @@ class Food(Base):
id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True, index=True)
serving_size = Column(String)
serving_size = Column(Float)
serving_unit = Column(String)
calories = Column(Float)
protein = Column(Float)
@@ -322,18 +322,7 @@ def calculate_meal_nutrition(meal, db: Session):
for meal_food in meal.meal_foods:
food = meal_food.food
grams = meal_food.quantity # quantity is now grams
# Convert grams to a multiplier of serving size for nutrition calculation
try:
serving_size_value = float(food.serving_size)
except ValueError:
serving_size_value = 1 # Fallback if serving_size is not a number
if serving_size_value == 0:
multiplier = 0 # Avoid division by zero
else:
multiplier = grams / serving_size_value
multiplier = meal_food.quantity
totals['calories'] += food.calories * multiplier
totals['protein'] += food.protein * multiplier