diff --git a/services/training/app/ml/prophet_manager.py b/services/training/app/ml/prophet_manager.py index 923e6da1..ebab84e7 100644 --- a/services/training/app/ml/prophet_manager.py +++ b/services/training/app/ml/prophet_manager.py @@ -801,12 +801,18 @@ class BakeryProphetManager: db_model.data_quality_score = float(training_metrics.get('data_quality_score')) if training_metrics.get('data_quality_score') is not None else None db_session.add(db_model) - await db_session.commit() - - logger.info(f"Model {model_id} stored in database successfully") + + # Only commit if using a new session, not if using parent session + # Parent session commits will be handled by the calling method to prevent conflicts + if not use_parent_session: + await db_session.commit() + logger.info(f"Model {model_id} stored in database successfully") + else: + logger.debug(f"Added model {model_id} to parent session (commit deferred)") try: # ✅ FIX: Use parent session if provided, otherwise create new one + # The parent session handles commits, so child sessions shouldn't commit if use_parent_session: logger.debug(f"Using parent session for storing model {model_id}") await _store_in_db(session) @@ -814,6 +820,9 @@ class BakeryProphetManager: logger.debug(f"Creating new session for storing model {model_id}") async with self.database_manager.get_session() as new_session: await _store_in_db(new_session) + # Commit only when using our own session (not parent) + await new_session.commit() # This is safe since new_session is dedicated + logger.info(f"Model {model_id} stored in database successfully") except Exception as e: logger.error(f"Failed to store model in database: {str(e)}")