From a3082037fad3721ffbbd295b6a9a1e4859ebe347 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Thu, 3 Jul 2025 08:10:42 -0700 Subject: [PATCH] fix: ensure loaded survey item responses are uniq by id to avoid PG::CardinalityViolation: ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time (PG::CardinalityViolation) --- app/services/survey_responses_data_loader.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/services/survey_responses_data_loader.rb b/app/services/survey_responses_data_loader.rb index dbdf0924..41667bd0 100644 --- a/app/services/survey_responses_data_loader.rb +++ b/app/services/survey_responses_data_loader.rb @@ -43,7 +43,11 @@ class SurveyResponsesDataLoader row_count += 1 next unless row_count == batch_size - SurveyItemResponse.import(survey_item_responses.compact.flatten, batch_size:, on_duplicate_key_update: :all) + survey_item_responses = survey_item_responses.compact.flatten.uniq do |response| + response.response_id + end + + SurveyItemResponse.import(survey_item_responses, batch_size:, on_duplicate_key_update: :all) survey_item_responses = [] unless file.eof? GC.start row_count = 0