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 8533449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:58:03+00:00 2026-06-11T09:58:03+00:00

First off, this is my first project using SQLAlchemy, so I’m still fairly new.

  • 0

First off, this is my first project using SQLAlchemy, so I’m still fairly new.

I am making a system to work with GTFS data. I have a back end that seems to be able to query the data quite efficiently.

What I am trying to do though is allow for the GTFS files to update the database with new data. The problem that I am hitting is pretty obvious, if the data I’m trying to insert is already in the database, we have a conflict on the uniqueness of the primary keys.

For Efficiency reasons, I decided to use the following code for insertions, where model is the model object I would like to insert the data into, and data is a precomputed, cleaned list of dictionaries to insert.

 for chunk in [data[i:i+chunk_size] for i in xrange(0, len(data), chunk_size)]:
    engine.execute(model.__table__.insert(),chunk)

There are two solutions that come to mind.

  1. I find a way to do the insert, such that if there is a collision, we don’t care, and don’t fail. I believe that the code above is using the TableClause, so I checked there first, hoping to find a suitable replacement, or flag, with no luck.

  2. Before we perform the cleaning of the data, we get the list of primary key values, and if a given element matches on the primary keys, we skip cleaning and inserting the value. I found that I was able to get the PrimaryKeyConstraint from Table.primary_key, but I can’t seem to get the Columns out, or find a way to query for only specific columns (in my case, the Primary Keys).

Either should be sufficient, if I can find a way to do it.

After looking into both of these for the last few hours, I can’t seem to find either. I was hoping that someone might have done this previously, and point me in the right direction.

Thanks in advance for your help!

Update 1: There is a 3rd option I failed to mention above. That is to purge all the data from the database, and reinsert it. I would prefer not to do this, as even with small GTFS files, there are easily hundreds of thousands of elements to insert, and this seems to take about half an hour to perform, which means if this makes it to production, lots of downtime for updates.

  • 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-11T09:58:04+00:00Added an answer on June 11, 2026 at 9:58 am

    With SQLAlchemy, you simply create a new instance of the model class, and merge it into the current session. SQLAlchemy will detect if it already knows about this object (from cache or the database) and will add a new row to the database if needed.

    newentry = model(chunk)
    session.merge(newentry)
    

    Also see this question for context: Fastest way to insert object if it doesn't exist with SQLAlchemy

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

Sidebar

Related Questions

First off tags wont work. I say this because i create 4 buttons all
I'm working on a relatively new project using Subversion. I've already branched off a
New to Stackoverflow, so first off, hello. I'm working on a little project for
I'm creating an expense report project - my first using MVC. This is a
First off here is some information, I am using Netbeans 7.0 with the project's
First off let me say this is my first ASP.NET project I have done.
First off I use this code to make the navigation bar always stay fixed;
First off the html row looks like this: <tr class=evenColor> blahblah TheTextIneed blahblah and
First off, this is NOT a duplicate of: Turn a C string with NULL
First off I would like to say that this is my first post and

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.