Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:26:14+00:00 2026-06-08T14:26:14+00:00

I have postgresql db which i am updating with around 100000 records. I use

  • 0

I have postgresql db which i am updating with around 100000 records. I use session.merge() to insert/update each record and i do a commit after every 1000 records.

i=0
for record in records:
    i+=1
    session.merge(record)
    if i%1000 == 0:
        session.commit()

This code works fine. In my database i have a table with a UNIQUE field and there are some duplicated records that i insert into it. A error is thrown when this happens, saying the field is not unique. Since i am inserting 1000 records at a time, a rollback will not help me to skip these records. is there any way i can skip the session.merge() for the duplicate records (other than parsing through all the records to find the duplicate records of course)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:26:15+00:00Added an answer on June 8, 2026 at 2:26 pm

    This is the option which works best for me because the number of records with duplicate unique keys is minimal.

    def update_exception(records, i, failed_records):
        failed_records.append(records[i]['pk'])
        session.rollback()
        start_range = int(round(i/1000,0) * 1000)
        for index in range(start_range, i+1):
            if records[index]['pk'] not in failed_records:
                ins_obj = Model()
                try:
                    session.merge(ins_obj)
                except:
                    failed_records.append(json_data[table_name][index-1]['pk'])
                    pass
    

    Say, if i hit an error at 2375 I store the primary key ‘pk’ for the 2375 record in failed_records and then i recommit from 2000 to 2375. It seems much faster than doing commits one by one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have postgresql (in perlu) function getTravelTime(integer, timestamp), which tries to select data for
I have a postgresql database and am using it for a project which handles
I have a server with PostgreSQL 8.4 which is being rebooted every night at
(I am using PostgreSQL) I have a table which stores transactions to an account.
I have a table in a postgresql-9.1.x database which is defined as follows: #
I have a script in PostgreSQL which restores test database from dump every night.
I have a column in the PostgreSQL database which contains Arabic data. When reading
I have a Postgresql dump file which i'm trying to restore. I get this
I have a PostgreSQL 9.1 database in which pictures are stored as large objects.
I have a PostgreSQL\PostGIS spatial database which contains Hebrew text columns. The system runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.