Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy error handling - how is it done?

Tags:

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?

like image 981
Hubro Avatar asked Jul 26 '12 04:07

Hubro


People also ask

How does the querying work with SQLAlchemy?

Python Flask and SQLAlchemy ORM All SELECT statements generated by SQLAlchemy ORM are constructed by Query object. It provides a generative interface, hence successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.

How does SQLAlchemy update data?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.

What is SQLAlchemyError?

class ArgumentError(SQLAlchemyError): """Raised when an invalid or conflicting function argument is supplied. This error generally corresponds to construction time state errors.

Is it possible to check and raise errors in SQLAlchemy?

However sqlalchemy already does that for you - so Is just the same. The check-and-raise part is already inside SQLAlchemy. ArgumentError - Raised when an invalid or conflicting function argument is supplied. This error generally corresponds to construction time state errors. DBAPIError - Raised when the execution of a database operation fails.

What are the exceptions used with SQLAlchemy?

Exceptions used with SQLAlchemy. The base exception class is SQLAlchemyError. Exceptions which are raised as a result of DBAPI exceptions are all subclasses of DBAPIError. exceptionsqlalchemy.exc.AmbiguousForeignKeysError(*arg, **kw)¶

How to check if SQLAlchemy is installed in Python?

To check if SQLAlchemy is properly installed and to know its version, enter the following command in the Python prompt − >>> import sqlalchemy >>>sqlalchemy.__version__ '1.2.7' SQLAlchemy Core – Expression Language SQLAlchemy core includes SQL rendering engine, DBAPI integration, transaction integration, and schema description services.

What is error handling in SQL Server?

Error handling overview Error handling in SQL Server gives us control over the Transact-SQL code. For example, when things go wrong, we get a chance to do something about it and possibly make it right again. SQL Server error handling can be as simple as just logging that something happened, or it could be us trying to fix an error.


Video Answer


1 Answers

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'")

like image 174
Hubro Avatar answered Oct 24 '22 00:10

Hubro