Core Exceptions

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.

exception sqlalchemy.exc.AmbiguousForeignKeysError

Raised when more than one foreign key matching can be located between two selectables during a join.

Class signature

class sqlalchemy.exc.AmbiguousForeignKeysError (sqlalchemy.exc.ArgumentError)

exception sqlalchemy.exc.ArgumentError

Raised when an invalid or conflicting function argument is supplied.

This error generally corresponds to construction time state errors.

Class signature

class sqlalchemy.exc.ArgumentError (sqlalchemy.exc.SQLAlchemyError)

exception sqlalchemy.exc.AwaitRequired

Error raised by the async greenlet spawn if no async operation was awaited when it required one.

Class signature

class sqlalchemy.exc.AwaitRequired (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.Base20DeprecationWarning

Issued for usage of APIs specifically deprecated or legacy in SQLAlchemy 2.0.

See also

The <some function> in SQLAlchemy 2.0 will no longer <something>.

SQLAlchemy 2.0 Deprecations Mode

Class signature

class sqlalchemy.exc.Base20DeprecationWarning (sqlalchemy.exc.SADeprecationWarning)

exception sqlalchemy.exc.CircularDependencyError

Raised by topological sorts when a circular dependency is detected.

There are two scenarios where this error occurs:

Class signature

class sqlalchemy.exc.CircularDependencyError (sqlalchemy.exc.SQLAlchemyError)

exception sqlalchemy.exc.CompileError

Raised when an error occurs during SQL compilation

Class signature

class sqlalchemy.exc.CompileError (sqlalchemy.exc.SQLAlchemyError)

exception sqlalchemy.exc.ConstraintColumnNotFoundError

raised when a constraint refers to a string column name that is not present in the table being constrained.

New in version 2.0.

Class signature

class sqlalchemy.exc.ConstraintColumnNotFoundError (sqlalchemy.exc.ArgumentError)

exception sqlalchemy.exc.DBAPIError

Raised when the execution of a database operation fails.

Wraps exceptions raised by the DB-API underlying the database operation. Driver-specific implementations of the standard DB-API exception types are wrapped by matching sub-types of SQLAlchemy’s DBAPIError when possible. DB-API’s Error type maps to DBAPIError in SQLAlchemy, otherwise the names are identical. Note that there is no guarantee that different DB-API implementations will raise the same exception type for any given error condition.

DBAPIError features StatementError.statement and StatementError.params attributes which supply context regarding the specifics of the statement which had an issue, for the typical case when the error was raised within the context of emitting a SQL statement.

The wrapped exception object is available in the StatementError.orig attribute. Its type and properties are DB-API implementation specific.

Class signature

class sqlalchemy.exc.DBAPIError (sqlalchemy.exc.StatementError)

  • method sqlalchemy.exc.DBAPIError.__init__(statement: Optional[str], params: Optional[_AnyExecuteParams], orig: BaseException, hide_parameters: bool = False, connection_invalidated: bool = False, code: Optional[str] = None, ismulti: Optional[bool] = None)

exception sqlalchemy.exc.DataError

Wraps a DB-API DataError.

Class signature

class sqlalchemy.exc.DataError (sqlalchemy.exc.DatabaseError)

exception sqlalchemy.exc.DatabaseError

Wraps a DB-API DatabaseError.

Class signature

class sqlalchemy.exc.DatabaseError (sqlalchemy.exc.DBAPIError)

exception sqlalchemy.exc.DisconnectionError

A disconnect is detected on a raw DB-API connection.

This error is raised and consumed internally by a connection pool. It can be raised by the PoolEvents.checkout() event so that the host pool forces a retry; the exception will be caught three times in a row before the pool gives up and raises InvalidRequestError regarding the connection attempt.

Class signature

class sqlalchemy.exc.DisconnectionError (sqlalchemy.exc.SQLAlchemyError)

Object NameDescription

DontWrapMixin

A mixin class which, when applied to a user-defined Exception class, will not be wrapped inside of StatementError if the error is emitted within the process of executing a statement.

HasDescriptionCode

helper which adds ‘code’ as an attribute and ‘_code_str’ as a method

class sqlalchemy.exc.DontWrapMixin

A mixin class which, when applied to a user-defined Exception class, will not be wrapped inside of StatementError if the error is emitted within the process of executing a statement.

E.g.:

  1. from sqlalchemy.exc import DontWrapMixin
  2. class MyCustomException(Exception, DontWrapMixin):
  3. pass
  4. class MySpecialType(TypeDecorator):
  5. impl = String
  6. def process_bind_param(self, value, dialect):
  7. if value == 'invalid':
  8. raise MyCustomException("invalid!")

exception sqlalchemy.exc.DuplicateColumnError

a Column is being added to a Table that would replace another Column, without appropriate parameters to allow this in place.

New in version 2.0.0b4.

Class signature

class sqlalchemy.exc.DuplicateColumnError (sqlalchemy.exc.ArgumentError)

class sqlalchemy.exc.HasDescriptionCode

helper which adds ‘code’ as an attribute and ‘_code_str’ as a method

exception sqlalchemy.exc.IdentifierError

Raised when a schema name is beyond the max character limit

Class signature

class sqlalchemy.exc.IdentifierError (sqlalchemy.exc.SQLAlchemyError)

exception sqlalchemy.exc.IllegalStateChangeError

An object that tracks state encountered an illegal state change of some kind.

New in version 2.0.

Class signature

class sqlalchemy.exc.IllegalStateChangeError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.IntegrityError

Wraps a DB-API IntegrityError.

Class signature

class sqlalchemy.exc.IntegrityError (sqlalchemy.exc.DatabaseError)

exception sqlalchemy.exc.InterfaceError

Wraps a DB-API InterfaceError.

Class signature

class sqlalchemy.exc.InterfaceError (sqlalchemy.exc.DBAPIError)

exception sqlalchemy.exc.InternalError

Wraps a DB-API InternalError.

Class signature

class sqlalchemy.exc.InternalError (sqlalchemy.exc.DatabaseError)

exception sqlalchemy.exc.InvalidRequestError

SQLAlchemy was asked to do something it can’t do.

This error generally corresponds to runtime state errors.

Class signature

class sqlalchemy.exc.InvalidRequestError (sqlalchemy.exc.SQLAlchemyError)

exception sqlalchemy.exc.InvalidatePoolError

Raised when the connection pool should invalidate all stale connections.

A subclass of DisconnectionError that indicates that the disconnect situation encountered on the connection probably means the entire pool should be invalidated, as the database has been restarted.

This exception will be handled otherwise the same way as DisconnectionError, allowing three attempts to reconnect before giving up.

New in version 1.2.

Class signature

class sqlalchemy.exc.InvalidatePoolError (sqlalchemy.exc.DisconnectionError)

exception sqlalchemy.exc.LegacyAPIWarning

indicates an API that is in ‘legacy’ status, a long term deprecation.

Class signature

class sqlalchemy.exc.LegacyAPIWarning (sqlalchemy.exc.Base20DeprecationWarning)

exception sqlalchemy.exc.MissingGreenlet

Error raised by the async greenlet await_ if called while not inside the greenlet spawn context.

Class signature

class sqlalchemy.exc.MissingGreenlet (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.MovedIn20Warning

Subtype of RemovedIn20Warning to indicate an API that moved only.

Class signature

class sqlalchemy.exc.MovedIn20Warning (sqlalchemy.exc.Base20DeprecationWarning)

exception sqlalchemy.exc.MultipleResultsFound

A single database result was required but more than one were found.

Changed in version 1.4: This exception is now part of the sqlalchemy.exc module in Core, moved from the ORM. The symbol remains importable from sqlalchemy.orm.exc.

Class signature

class sqlalchemy.exc.MultipleResultsFound (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.NoForeignKeysError

Raised when no foreign keys can be located between two selectables during a join.

Class signature

class sqlalchemy.exc.NoForeignKeysError (sqlalchemy.exc.ArgumentError)

exception sqlalchemy.exc.NoInspectionAvailable

A subject passed to sqlalchemy.inspection.inspect() produced no context for inspection.

Class signature

class sqlalchemy.exc.NoInspectionAvailable (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.NoReferenceError

Raised by ForeignKey to indicate a reference cannot be resolved.

Class signature

class sqlalchemy.exc.NoReferenceError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.NoReferencedColumnError

Raised by ForeignKey when the referred Column cannot be located.

Class signature

class sqlalchemy.exc.NoReferencedColumnError (sqlalchemy.exc.NoReferenceError)

exception sqlalchemy.exc.NoReferencedTableError

Raised by ForeignKey when the referred Table cannot be located.

Class signature

class sqlalchemy.exc.NoReferencedTableError (sqlalchemy.exc.NoReferenceError)

exception sqlalchemy.exc.NoResultFound

A database result was required but none was found.

Changed in version 1.4: This exception is now part of the sqlalchemy.exc module in Core, moved from the ORM. The symbol remains importable from sqlalchemy.orm.exc.

Class signature

class sqlalchemy.exc.NoResultFound (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.NoSuchColumnError

A nonexistent column is requested from a Row.

Class signature

class sqlalchemy.exc.NoSuchColumnError (sqlalchemy.exc.InvalidRequestError, builtins.KeyError)

exception sqlalchemy.exc.NoSuchModuleError

Raised when a dynamically-loaded module (usually a database dialect) of a particular name cannot be located.

Class signature

class sqlalchemy.exc.NoSuchModuleError (sqlalchemy.exc.ArgumentError)

exception sqlalchemy.exc.NoSuchTableError

Table does not exist or is not visible to a connection.

Class signature

class sqlalchemy.exc.NoSuchTableError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.NotSupportedError

Wraps a DB-API NotSupportedError.

Class signature

class sqlalchemy.exc.NotSupportedError (sqlalchemy.exc.DatabaseError)

exception sqlalchemy.exc.ObjectNotExecutableError

Raised when an object is passed to .execute() that can’t be executed as SQL.

New in version 1.1.

Class signature

class sqlalchemy.exc.ObjectNotExecutableError (sqlalchemy.exc.ArgumentError)

exception sqlalchemy.exc.OperationalError

Wraps a DB-API OperationalError.

Class signature

class sqlalchemy.exc.OperationalError (sqlalchemy.exc.DatabaseError)

exception sqlalchemy.exc.PendingRollbackError

A transaction has failed and needs to be rolled back before continuing.

New in version 1.4.

Class signature

class sqlalchemy.exc.PendingRollbackError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.ProgrammingError

Wraps a DB-API ProgrammingError.

Class signature

class sqlalchemy.exc.ProgrammingError (sqlalchemy.exc.DatabaseError)

exception sqlalchemy.exc.ResourceClosedError

An operation was requested from a connection, cursor, or other object that’s in a closed state.

Class signature

class sqlalchemy.exc.ResourceClosedError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.SADeprecationWarning

Issued for usage of deprecated APIs.

Class signature

class sqlalchemy.exc.SADeprecationWarning (sqlalchemy.exc.HasDescriptionCode, builtins.DeprecationWarning)

exception sqlalchemy.exc.SAPendingDeprecationWarning

A similar warning as SADeprecationWarning, this warning is not used in modern versions of SQLAlchemy.

Class signature

class sqlalchemy.exc.SAPendingDeprecationWarning (builtins.PendingDeprecationWarning)

exception sqlalchemy.exc.SATestSuiteWarning

warning for a condition detected during tests that is non-fatal

Currently outside of SAWarning so that we can work around tools like Alembic doing the wrong thing with warnings.

Class signature

class sqlalchemy.exc.SATestSuiteWarning (builtins.Warning)

exception sqlalchemy.exc.SAWarning

Issued at runtime.

Class signature

class sqlalchemy.exc.SAWarning (sqlalchemy.exc.HasDescriptionCode, builtins.RuntimeWarning)

exception sqlalchemy.exc.SQLAlchemyError

Generic error class.

Class signature

class sqlalchemy.exc.SQLAlchemyError (sqlalchemy.exc.HasDescriptionCode, builtins.Exception)

exception sqlalchemy.exc.StatementError

An error occurred during execution of a SQL statement.

StatementError wraps the exception raised during execution, and features statement and params attributes which supply context regarding the specifics of the statement which had an issue.

The wrapped exception object is available in the orig attribute.

Class signature

class sqlalchemy.exc.StatementError (sqlalchemy.exc.SQLAlchemyError)

  • method sqlalchemy.exc.StatementError.__init__(message: str, statement: Optional[str], params: Optional[_AnyExecuteParams], orig: Optional[BaseException], hide_parameters: bool = False, code: Optional[str] = None, ismulti: Optional[bool] = None)

  • attribute sqlalchemy.exc.StatementError.ismulti: Optional[bool] = None

    multi parameter passed to repr_params(). None is meaningful.

  • attribute sqlalchemy.exc.StatementError.orig: Optional[BaseException] = None

    The original exception that was thrown.

  • attribute sqlalchemy.exc.StatementError.params: Optional[_AnyExecuteParams] = None

    The parameter list being used when this exception occurred.

  • attribute sqlalchemy.exc.StatementError.statement: Optional[str] = None

    The string SQL statement being invoked when this exception occurred.

exception sqlalchemy.exc.TimeoutError

Raised when a connection pool times out on getting a connection.

Class signature

class sqlalchemy.exc.TimeoutError (sqlalchemy.exc.SQLAlchemyError)

exception sqlalchemy.exc.UnboundExecutionError

SQL was attempted without a database connection to execute it on.

Class signature

class sqlalchemy.exc.UnboundExecutionError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.UnreflectableTableError

Table exists but can’t be reflected for some reason.

New in version 1.2.

Class signature

class sqlalchemy.exc.UnreflectableTableError (sqlalchemy.exc.InvalidRequestError)

exception sqlalchemy.exc.UnsupportedCompilationError

Raised when an operation is not supported by the given compiler.

See also

How do I render SQL expressions as strings, possibly with bound parameters inlined?

Compiler StrSQLCompiler can’t render element of type <element type>

Class signature

class sqlalchemy.exc.UnsupportedCompilationError (sqlalchemy.exc.CompileError)