{"record":{"id":"9e50f876f0135d8c","repo":"ankane/pgsync","slug":"primary-key-required-for-overwrite","errorCode":null,"errorMessage":"Primary key required for --overwrite","messagePattern":"Primary key required for --overwrite","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/pgsync/task.rb","lineNumber":129,"sourceCode":"          where = \"#{quote_ident(primary_key)} >= #{starting_id} AND #{quote_ident(primary_key)} < #{starting_id + batch_size}\"\n          log \"    #{i}/#{batch_count}: #{where}\"\n\n          # TODO be smarter for advance sql clauses\n          batch_sql_clause = \" #{sql_clause.length > 0 ? \"#{sql_clause} AND\" : \"WHERE\"} #{where}\"\n\n          batch_copy_to_command = \"COPY (SELECT #{copy_fields} FROM #{quoted_table}#{batch_sql_clause}) TO STDOUT\"\n          copy(batch_copy_to_command, dest_table: table, dest_fields: fields)\n\n          starting_id += batch_size\n          i += 1\n\n          if opts[:sleep] && starting_id <= from_max_id\n            sleep(opts[:sleep])\n          end\n        end\n      elsif !opts[:truncate] && (opts[:overwrite] || opts[:preserve] || !sql_clause.empty?)\n        if primary_key.empty?\n          raise Error, \"Primary key required for --overwrite\" if opts[:overwrite]\n          raise Error, \"Primary key required for --preserve\" if opts[:preserve]\n          raise Error, \"Primary key required to sync specific rows\"\n        end\n\n        # create a temp table\n        temp_table = \"pgsync_#{rand(1_000_000_000)}\"\n        destination.execute(\"CREATE TEMPORARY TABLE #{quote_ident_full(temp_table)} AS TABLE #{quoted_table} WITH NO DATA\")\n\n        # load data\n        copy(copy_to_command, dest_table: temp_table, dest_fields: fields)\n\n        on_conflict = primary_key.map { |pk| quote_ident(pk) }.join(\", \")\n        action =\n          if opts[:preserve]\n            \"NOTHING\"\n          else # overwrite or sql clause\n            setter = shared_fields.reject { |f| primary_key.include?(f) }.map { |f| \"#{quote_ident(f)} = EXCLUDED.#{quote_ident(f)}\" }\n            if setter.any?","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/ankane/pgsync/blob/57bdddf5db506d329b51cb3376379dc3a2559010/lib/pgsync/task.rb#L111-L147","documentation":"Error \"Primary key required for --overwrite\" thrown in ankane/pgsync.","triggerScenarios":"Thrown at lib/pgsync/task.rb:129 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"57bdddf5db506d329b51cb3376379dc3a2559010","analyzedAt":"2026-08-23T11:18:48.887Z","schemaVersion":2},"datasetVersion":"2026-08-23T16:17:53.355Z"}