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
.
- exceptionsqlalchemy.exc.AmbiguousForeignKeysError¶
inherits from
sqlalchemy.exc.ArgumentError
Raised when more than one foreign key matching can be located between two selectables during a join.
- exceptionsqlalchemy.exc.ArgumentError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
Raised when an invalid or conflicting function argument is supplied.
This error generally corresponds to construction time state errors.
- exceptionsqlalchemy.exc.AwaitRequired¶
inherits from
sqlalchemy.exc.InvalidRequestError
Error raised by the async greenlet spawn if no async operation was awaited when it required one.
- exceptionsqlalchemy.exc.Base20DeprecationWarning¶
inherits from
sqlalchemy.exc.SADeprecationWarning
Issued for usage of APIs specifically deprecated or legacy in SQLAlchemy 2.0.
-
attribute
sqlalchemy.exc.Base20DeprecationWarning.
deprecated_since:str|None='1.4'¶ Indicates the version that started raising this deprecation warning
-
attribute
- exceptionsqlalchemy.exc.CircularDependencyError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
Raised by topological sorts when a circular dependency is detected.
There are two scenarios where this error occurs:
In a Session flush operation, if two objects are mutually dependent on each other, they can not be inserted or deleted via INSERT or DELETE statements alone; an UPDATE will be needed to post-associate or pre-deassociate one of the foreign key constrained values. The
post_update
flag described at Rows that point to themselves / Mutually Dependent Rows can resolve this cycle.In a
MetaData.sorted_tables
operation, twoForeignKey
orForeignKeyConstraint
objects mutually refer to each other. Apply theuse_alter=True
flag to one or both, see Creating/Dropping Foreign Key Constraints via ALTER.
-
method
sqlalchemy.exc.CircularDependencyError.
__init__(message:str, cycles:Any, edges:Any, msg:str|None=None, code:str|None=None)¶
- exceptionsqlalchemy.exc.CompileError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
Raised when an error occurs during SQL compilation
- exceptionsqlalchemy.exc.ConstraintColumnNotFoundError¶
inherits from
sqlalchemy.exc.ArgumentError
raised when a constraint refers to a string column name that is not present in the table being constrained.
Added in version 2.0.
- exceptionsqlalchemy.exc.DBAPIError¶
inherits from
sqlalchemy.exc.StatementError
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’sError
type maps toDBAPIError
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
featuresStatementError.statement
andStatementError.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.-
method
sqlalchemy.exc.DBAPIError.
__init__(statement:str|None, params:_AnyExecuteParams|None, orig:BaseException, hide_parameters:bool=False, connection_invalidated:bool=False, code:str|None=None, ismulti:bool|None=None)¶
-
attribute
sqlalchemy.exc.DBAPIError.
driver_exception¶ The exception object originating from the driver (DBAPI) outside of SQLAlchemy.
In the case of some asyncio dialects, special steps are taken to resolve the exception to what the third party driver has raised, even for SQLAlchemy dialects that include an "emulated" DBAPI exception hierarchy.
For non-asyncio dialects, this attribute will be the same attribute as the
StatementError.orig
attribute.For an asyncio dialect provided by SQLAlchemy, depending on if the dialect provides an "emulated" exception hierarchy or if the underlying DBAPI raises DBAPI-style exceptions, it will refer to either the
EmulatedDBAPIException.driver_exception
attribute on theEmulatedDBAPIException
that’s thrown (such as when using asyncpg), or to the actual exception object thrown by the third party driver.Added in version 2.1.
-
method
- exceptionsqlalchemy.exc.DataError¶
inherits from
sqlalchemy.exc.DatabaseError
Wraps a DB-API DataError.
- exceptionsqlalchemy.exc.DatabaseError¶
inherits from
sqlalchemy.exc.DBAPIError
Wraps a DB-API DatabaseError.
- exceptionsqlalchemy.exc.DisconnectionError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
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 raisesInvalidRequestError
regarding the connection attempt.
Object Name | Description |
---|---|
A mixin class which, when applied to a user-defined Exception class,
will not be wrapped inside of |
|
helper which adds ‘code’ as an attribute and ‘_code_str’ as a method |
- classsqlalchemy.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.:
fromsqlalchemy.excimport DontWrapMixin classMyCustomException(Exception, DontWrapMixin): pass classMySpecialType(TypeDecorator): impl = String defprocess_bind_param(self, value, dialect): if value == "invalid": raise MyCustomException("invalid!")
- exceptionsqlalchemy.exc.DuplicateColumnError¶
inherits from
sqlalchemy.exc.ArgumentError
a Column is being added to a Table that would replace another Column, without appropriate parameters to allow this in place.
Added in version 2.0.0b4.
- exceptionsqlalchemy.exc.EmulatedDBAPIException¶
inherits from
builtins.Exception
Serves as the base of the DBAPI
Error
class for dialects where a DBAPI exception hierrchy needs to be emulated.The current example is the asyncpg dialect.
Added in version 2.1.
-
method
sqlalchemy.exc.EmulatedDBAPIException.
__init__(message:str, orig:Exception|None=None)¶
-
attribute
sqlalchemy.exc.EmulatedDBAPIException.
driver_exception¶ The original driver exception that was raised.
This exception object will always originate from outside of SQLAlchemy.
-
method
- classsqlalchemy.exc.HasDescriptionCode¶
helper which adds ‘code’ as an attribute and ‘_code_str’ as a method
- exceptionsqlalchemy.exc.IdentifierError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
Raised when a schema name is beyond the max character limit
- exceptionsqlalchemy.exc.IllegalStateChangeError¶
inherits from
sqlalchemy.exc.InvalidRequestError
An object that tracks state encountered an illegal state change of some kind.
Added in version 2.0.
- exceptionsqlalchemy.exc.IntegrityError¶
inherits from
sqlalchemy.exc.DatabaseError
Wraps a DB-API IntegrityError.
- exceptionsqlalchemy.exc.InterfaceError¶
inherits from
sqlalchemy.exc.DBAPIError
Wraps a DB-API InterfaceError.
- exceptionsqlalchemy.exc.InternalError¶
inherits from
sqlalchemy.exc.DatabaseError
Wraps a DB-API InternalError.
- exceptionsqlalchemy.exc.InvalidRequestError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
SQLAlchemy was asked to do something it can’t do.
This error generally corresponds to runtime state errors.
- exceptionsqlalchemy.exc.InvalidatePoolError¶
inherits from
sqlalchemy.exc.DisconnectionError
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.
- exceptionsqlalchemy.exc.LegacyAPIWarning¶
inherits from
sqlalchemy.exc.Base20DeprecationWarning
indicates an API that is in ‘legacy’ status, a long term deprecation.
- exceptionsqlalchemy.exc.MissingGreenlet¶
inherits from
sqlalchemy.exc.InvalidRequestError
Error raised by the async greenlet await_ if called while not inside the greenlet spawn context.
- exceptionsqlalchemy.exc.MovedIn20Warning¶
inherits from
sqlalchemy.exc.Base20DeprecationWarning
Subtype of Base20DeprecationWarning to indicate an API that moved only.
- exceptionsqlalchemy.exc.MultipleResultsFound¶
inherits from
sqlalchemy.exc.InvalidRequestError
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 fromsqlalchemy.orm.exc
.
- exceptionsqlalchemy.exc.NoForeignKeysError¶
inherits from
sqlalchemy.exc.ArgumentError
Raised when no foreign keys can be located between two selectables during a join.
- exceptionsqlalchemy.exc.NoInspectionAvailable¶
inherits from
sqlalchemy.exc.InvalidRequestError
A subject passed to
sqlalchemy.inspection.inspect()
produced no context for inspection.
- exceptionsqlalchemy.exc.NoReferenceError¶
inherits from
sqlalchemy.exc.InvalidRequestError
Raised by
ForeignKey
to indicate a reference cannot be resolved.
- exceptionsqlalchemy.exc.NoReferencedColumnError¶
inherits from
sqlalchemy.exc.NoReferenceError
Raised by
ForeignKey
when the referredColumn
cannot be located.-
method
sqlalchemy.exc.NoReferencedColumnError.
__init__(message:str, tname:str, cname:str)¶
-
method
- exceptionsqlalchemy.exc.NoReferencedTableError¶
inherits from
sqlalchemy.exc.NoReferenceError
Raised by
ForeignKey
when the referredTable
cannot be located.-
method
sqlalchemy.exc.NoReferencedTableError.
__init__(message:str, tname:str)¶
-
method
- exceptionsqlalchemy.exc.NoResultFound¶
inherits from
sqlalchemy.exc.InvalidRequestError
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 fromsqlalchemy.orm.exc
.
- exceptionsqlalchemy.exc.NoSuchColumnError¶
inherits from
sqlalchemy.exc.InvalidRequestError
,builtins.KeyError
A nonexistent column is requested from a
Row
.
- exceptionsqlalchemy.exc.NoSuchModuleError¶
inherits from
sqlalchemy.exc.ArgumentError
Raised when a dynamically-loaded module (usually a database dialect) of a particular name cannot be located.
- exceptionsqlalchemy.exc.NoSuchTableError¶
inherits from
sqlalchemy.exc.InvalidRequestError
Table does not exist or is not visible to a connection.
- exceptionsqlalchemy.exc.NotSupportedError¶
inherits from
sqlalchemy.exc.DatabaseError
Wraps a DB-API NotSupportedError.
- exceptionsqlalchemy.exc.ObjectNotExecutableError¶
inherits from
sqlalchemy.exc.ArgumentError
Raised when an object is passed to .execute() that can’t be executed as SQL.
-
method
sqlalchemy.exc.ObjectNotExecutableError.
__init__(target:Any)¶
-
method
- exceptionsqlalchemy.exc.OperationalError¶
inherits from
sqlalchemy.exc.DatabaseError
Wraps a DB-API OperationalError.
- exceptionsqlalchemy.exc.PendingRollbackError¶
inherits from
sqlalchemy.exc.InvalidRequestError
A transaction has failed and needs to be rolled back before continuing.
Added in version 1.4.
- exceptionsqlalchemy.exc.ProgrammingError¶
inherits from
sqlalchemy.exc.DatabaseError
Wraps a DB-API ProgrammingError.
- exceptionsqlalchemy.exc.ResourceClosedError¶
inherits from
sqlalchemy.exc.InvalidRequestError
An operation was requested from a connection, cursor, or other object that’s in a closed state.
- exceptionsqlalchemy.exc.SADeprecationWarning¶
inherits from
sqlalchemy.exc.HasDescriptionCode
,builtins.DeprecationWarning
Issued for usage of deprecated APIs.
-
attribute
sqlalchemy.exc.SADeprecationWarning.
deprecated_since:str|None=None¶ Indicates the version that started raising this deprecation warning
-
attribute
- exceptionsqlalchemy.exc.SAPendingDeprecationWarning¶
inherits from
builtins.PendingDeprecationWarning
A similar warning as
SADeprecationWarning
, this warning is not used in modern versions of SQLAlchemy.-
attribute
sqlalchemy.exc.SAPendingDeprecationWarning.
deprecated_since:str|None=None¶ Indicates the version that started raising this deprecation warning
-
attribute
- exceptionsqlalchemy.exc.SATestSuiteWarning¶
inherits from
builtins.Warning
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.
- exceptionsqlalchemy.exc.SAWarning¶
inherits from
sqlalchemy.exc.HasDescriptionCode
,builtins.RuntimeWarning
Issued at runtime.
- exceptionsqlalchemy.exc.SQLAlchemyError¶
inherits from
sqlalchemy.exc.HasDescriptionCode
,builtins.Exception
Generic error class.
- exceptionsqlalchemy.exc.StatementError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
An error occurred during execution of a SQL statement.
StatementError
wraps the exception raised during execution, and featuresstatement
andparams
attributes which supply context regarding the specifics of the statement which had an issue.The wrapped exception object is available in the
orig
attribute.-
method
sqlalchemy.exc.StatementError.
__init__(message:str, statement:str|None, params:_AnyExecuteParams|None, orig:BaseException|None, hide_parameters:bool=False, code:str|None=None, ismulti:bool|None=None)¶
-
attribute
sqlalchemy.exc.StatementError.
ismulti:bool|None=None¶ multi parameter passed to repr_params(). None is meaningful.
-
attribute
sqlalchemy.exc.StatementError.
orig:BaseException|None=None¶ The original exception that was thrown.
See also
DBAPIError.driver_exception
- a more specific attribute that is guaranteed to return the exception object raised by the third party driver in use, even when using asyncio.
-
attribute
sqlalchemy.exc.StatementError.
params:_AnyExecuteParams|None=None¶ The parameter list being used when this exception occurred.
-
attribute
sqlalchemy.exc.StatementError.
statement:str|None=None¶ The string SQL statement being invoked when this exception occurred.
-
method
- exceptionsqlalchemy.exc.TimeoutError¶
inherits from
sqlalchemy.exc.SQLAlchemyError
Raised when a connection pool times out on getting a connection.
- exceptionsqlalchemy.exc.UnboundExecutionError¶
inherits from
sqlalchemy.exc.InvalidRequestError
SQL was attempted without a database connection to execute it on.
- exceptionsqlalchemy.exc.UnreflectableTableError¶
inherits from
sqlalchemy.exc.InvalidRequestError
Table exists but can’t be reflected for some reason.
- exceptionsqlalchemy.exc.UnsupportedCompilationError¶
inherits from
sqlalchemy.exc.CompileError
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>
-
method
sqlalchemy.exc.UnsupportedCompilationError.
__init__(compiler:Compiled |TypeCompiler, element_type:Type[ClauseElement ], message:str|None=None)¶
-
method