NoInspectionAvailable
is a class within the sqlalchemy.exc
module of the SQLAlchemy project.
ArgumentError,
DataError,
DatabaseError,
IntegrityError,
InvalidRequestError,
NoSuchTableError,
OperationalError,
ProgrammingError,
and UnsupportedCompilationError
are several other callables with code examples from the same sqlalchemy.exc
package.
sqlalchemy-utils (project documentation and PyPI package information) is a code library with various helper functions and new data types that make it easier to use SQLAlchemy when building projects that involve more specific storage requirements such as currency. The wide array of data types includes ranged values and aggregated attributes.
sqlalchemy-utils / sqlalchemy_utils / functions / foreign_keys.py
# foreign_keys.py
from collections import defaultdict
from itertools import groupby
import sqlalchemy as sa
from sqlalchemy.exc import NoInspectionAvailable
from sqlalchemy.orm import object_session
from sqlalchemy.schema import ForeignKeyConstraint, MetaData, Table
from ..query_chain import QueryChain
from .database import has_index
from .orm import get_column_key, get_mapper, get_tables
def get_foreign_key_values(fk, obj):
return dict(
(
fk.constraint.columns.values()[index].key,
getattr(obj, element.column.key)
)
for
index, element
in
enumerate(fk.constraint.elements)
)
def group_foreign_keys(foreign_keys):
foreign_keys = sorted(
foreign_keys, key=lambda key: key.constraint.table.name
## ... source file abbreviated to get to NoInspectionAvailable examples ...
else:
(
session.query(mapper.class_)
.filter_by(**old_values)
.update(
new_values,
'evaluate'
)
)
def dependent_objects(obj, foreign_keys=None):
if foreign_keys is None:
foreign_keys = get_referencing_foreign_keys(obj)
session = object_session(obj)
chain = QueryChain([])
classes = obj.__class__._decl_class_registry
for table, keys in group_foreign_keys(foreign_keys):
keys = list(keys)
for class_ in classes.values():
try:
mapper = sa.inspect(class_)
except NoInspectionAvailable:
continue
parent_mapper = mapper.inherits
if (
table in mapper.tables and
not (parent_mapper and table in parent_mapper.tables)
):
query = session.query(class_).filter(
sa.or_(*_get_criteria(keys, class_, obj))
)
chain.queries.append(query)
return chain
def _get_criteria(keys, class_, obj):
criteria = []
visited_constraints = []
for key in keys:
if key.constraint in visited_constraints:
continue
visited_constraints.append(key.constraint)
subcriteria = []
for index, column in enumerate(key.constraint.columns):
foreign_column = (
## ... source file continues with no further NoInspectionAvailable examples...