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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:13:05+00:00 2026-06-08T16:13:05+00:00

I want to handle a case where there’s a primary key or unique key

  • 0

I want to handle a case where there’s a primary key or unique key conflict, a.k.a. a duplicate entry. For this I’m catching the IntegrityError, which catches the error just fine. The problem is, I can’t seem to find any simple error message or error code to check for. All I’m getting is the IntegrityError.message property which is a string that looks like this:

(IntegrityError) (1062, “Duplicate entry ‘foobar’ for key ‘name'”)

That’s not very helpful. Using that I’m going to have to start parsing error messages for their code and message. Calling dir on the exception shows only the following properties:

‘args’, ‘connection_invalidated’, ‘instance’, ‘message’, ‘orig’, ‘params’, ‘statement’

args is simply a single-item tuple with the aforementioned string inside it and params is the data I tried to insert. I can’t seem to find any way of determining that this actually is a duplicate key error without having to start parsing the error message using regex or something.

Can anyone shed some light on this issue?

  • 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-08T16:13:10+00:00Added an answer on June 8, 2026 at 4:13 pm

    I figured this out while writing the question by reading the documentation more carefully. I’m still going to post this though since it might be of help to someone.

    In the documentation of the SQLAlchemy DBAPIError, from which the IntegrityError is subclassed, it explains that the exception is merely a wrapper for the underlying database API error and that the original error is saved as orig in the exception. Sure enough, calling e.orig.args I get a nicely organized tuple:

    (1062, “Duplicate entry ‘foobar’ for key ‘name'”)

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

Sidebar

Related Questions

I want to create a live, checkers-like app, which will work like this: There
I want to handle the focus out Handler only to the component which i
i want to handle the tab keypress in such a way that if there
I have an input box which I want to handle two types of information,
I need to (safely) handle the case where there is < 5 reviews for
render @some_object will render Rails.root/app/views/some_objects/_some_object.html.erb Now I want to handle which partial will be
I want to handle mouse click in a native MFC application from a C#
I want to handle the keydown event globally in my application and as such
I want to handle events on a treeview with ACB ( http://marlongrech.wordpress.com/2008/12/04/attachedcommandbehavior-aka-acb/ ). I
I have an ATL app where I want to handle WM_POWERBROADCAST . I have

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.