mirror of
https://github.com/thecyberlearn/quantum-ai-v2.git
synced 2026-08-18 21:52:58 +00:00
Fix UNIQUE constraint error in Data Analyzer
- Replace objects.create() with get_or_create() to prevent duplicate responses - Handle case where response already exists by updating it instead of creating new one - Fix both success and error response creation - Prevents 'UNIQUE constraint failed: data_analyzer_responses.request_id' error Issue: OneToOneField relationship allows only one response per request Solution: Use get_or_create() pattern to handle existing responses gracefully
This commit is contained in:
parent
f6ba431514
commit
65c713ef64
@ -106,17 +106,29 @@ class DataAnalysisAgentProcessor(StandardWebhookProcessor):
|
|||||||
# Determine success based on N8N status
|
# Determine success based on N8N status
|
||||||
success = status == 'success' and bool(analysis_text)
|
success = status == 'success' and bool(analysis_text)
|
||||||
|
|
||||||
# Create response object
|
# Create or update response object (prevent duplicate responses)
|
||||||
response_obj = DataAnalysisAgentResponse.objects.create(
|
response_obj, created = DataAnalysisAgentResponse.objects.get_or_create(
|
||||||
request=request_obj,
|
request=request_obj,
|
||||||
success=success,
|
defaults={
|
||||||
processing_time=response_data.get('processing_time', 0),
|
'success': success,
|
||||||
analysis_results=analysis_results,
|
'processing_time': response_data.get('processing_time', 0),
|
||||||
insights_summary=insights_summary,
|
'analysis_results': analysis_results,
|
||||||
report_text=report_text,
|
'insights_summary': insights_summary,
|
||||||
raw_response=raw_response,
|
'report_text': report_text,
|
||||||
|
'raw_response': raw_response,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# If response already exists, update it
|
||||||
|
if not created:
|
||||||
|
response_obj.success = success
|
||||||
|
response_obj.processing_time = response_data.get('processing_time', 0)
|
||||||
|
response_obj.analysis_results = analysis_results
|
||||||
|
response_obj.insights_summary = insights_summary
|
||||||
|
response_obj.report_text = report_text
|
||||||
|
response_obj.raw_response = raw_response
|
||||||
|
response_obj.save()
|
||||||
|
|
||||||
# Only deduct wallet balance after successful processing
|
# Only deduct wallet balance after successful processing
|
||||||
if success:
|
if success:
|
||||||
request_obj.user.deduct_balance(
|
request_obj.user.deduct_balance(
|
||||||
@ -138,12 +150,21 @@ class DataAnalysisAgentProcessor(StandardWebhookProcessor):
|
|||||||
request_obj.status = 'failed'
|
request_obj.status = 'failed'
|
||||||
request_obj.save()
|
request_obj.save()
|
||||||
|
|
||||||
# Create error response
|
# Create or update error response (prevent duplicate responses)
|
||||||
error_response = DataAnalysisAgentResponse.objects.create(
|
error_response, created = DataAnalysisAgentResponse.objects.get_or_create(
|
||||||
request=request_obj,
|
request=request_obj,
|
||||||
success=False,
|
defaults={
|
||||||
error_message=str(e),
|
'success': False,
|
||||||
processing_time=response_data.get('processing_time', 0) if response_data else 0
|
'error_message': str(e),
|
||||||
|
'processing_time': response_data.get('processing_time', 0) if response_data else 0
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# If response already exists, update it with error info
|
||||||
|
if not created:
|
||||||
|
error_response.success = False
|
||||||
|
error_response.error_message = str(e)
|
||||||
|
error_response.processing_time = response_data.get('processing_time', 0) if response_data else 0
|
||||||
|
error_response.save()
|
||||||
|
|
||||||
raise Exception(f"Failed to process Data Analysis Agent response: {e}")
|
raise Exception(f"Failed to process Data Analysis Agent response: {e}")
|
||||||
Loading…
Reference in New Issue
Block a user