class documentation

class Inspector(object):

Known subclasses: sqlalchemy.databases.postgresql.PGInspector, sqlalchemy.databases.sybase.SybaseInspector

View In Hierarchy

Performs database schema inspection.

The Inspector acts as a proxy to the reflection methods of the ~sqlalchemy.engine.interfaces.Dialect, providing a consistent interface as well as caching support for previously fetched metadata.

A _reflection.Inspector object is usually created via the _sa.inspect function, which may be passed an _engine.Engine or a _engine.Connection:

from sqlalchemy import inspect, create_engine
engine = create_engine('...')
insp = inspect(engine)

Where above, the ~sqlalchemy.engine.interfaces.Dialect associated with the engine may opt to return an _reflection.Inspector subclass that provides additional methods specific to the dialect's target database.

Class Method from​_engine Construct a new dialect-specific Inspector object from the given engine or connection.
Method __init__ Initialize a new _reflection.Inspector.
Method get​_check​_constraints Return information about check constraints in table_name.
Method get​_columns Return information about columns in table_name.
Method get​_foreign​_keys Return information about foreign_keys in table_name.
Method get​_indexes Return information about indexes in table_name.
Method get​_pk​_constraint Return information about primary key constraint on table_name.
Method get​_schema​_names Return all schema names.
Method get​_sequence​_names Return all sequence names in schema.
Method get​_sorted​_table​_and​_fkc​_names Return dependency-sorted table and foreign key constraint names in referred to within a particular schema.
Method get​_table​_comment Return information about the table comment for table_name.
Method get​_table​_names Return all table names in referred to within a particular schema.
Method get​_table​_options Return a dictionary of options specified when the table of the given name was created.
Method get​_temp​_table​_names Return a list of temporary table names for the current bind.
Method get​_temp​_view​_names Return a list of temporary view names for the current bind.
Method get​_unique​_constraints Return information about unique constraints in table_name.
Method get​_view​_definition Return definition for view_name.
Method get​_view​_names Return all view names in schema.
Method has​_sequence Return True if the backend has a table of the given name.
Method has​_table Return True if the backend has a table of the given name.
Method reflect​_table Given a _schema.Table object, load its internal constructs based on introspection.
Method reflecttable See reflect_table. This method name is deprecated
Class Method ​_construct Undocumented
Method ​_connectable​_insp Undocumented
Method ​_connection​_insp Undocumented
Method ​_engine​_insp Undocumented
Method ​_init​_connection Undocumented
Method ​_init​_engine Undocumented
Method ​_init​_legacy Undocumented
Method ​_inspection​_context Return an _reflection.Inspector from this one that will run all operations on a single connection.
Method ​_operation​_context Return a context that optimizes for multiple operations on a single transaction.
Method ​_reflect​_check​_constraints Undocumented
Method ​_reflect​_col​_sequence Undocumented
Method ​_reflect​_column Undocumented
Method ​_reflect​_fk Undocumented
Method ​_reflect​_indexes Undocumented
Method ​_reflect​_pk Undocumented
Method ​_reflect​_table​_comment Undocumented
Method ​_reflect​_unique​_constraints Undocumented
Class Variable ​_index​_sort​_exprs Undocumented
Instance Variable ​_op​_context​_requires​_connect Undocumented
Instance Variable bind Undocumented
Instance Variable dialect Undocumented
Instance Variable engine Undocumented
Instance Variable info​_cache Undocumented
Property default​_schema​_name Return the default schema name presented by the dialect for the current engine's database user.
@classmethod
@util.deprecated('1.4', 'The from_engine() method on :class:`_reflection.Inspector` is deprecated and will be removed in a future release. Please use the :func:`.sqlalchemy.inspect` function on an :class:`_engine.Engine` or :class:`_engine.Connection` in order to acquire an :class:`_reflection.Inspector`.')
def from_engine(cls, bind):

Construct a new dialect-specific Inspector object from the given engine or connection.

This method differs from direct a direct constructor call of _reflection.Inspector in that the ~sqlalchemy.engine.interfaces.Dialect is given a chance to provide a dialect-specific _reflection.Inspector instance, which may provide additional methods.

See the example at _reflection.Inspector.

Parameters
binda ~sqlalchemy.engine.Connectable, which is typically an instance of ~sqlalchemy.engine.Engine or ~sqlalchemy.engine.Connection.
@util.deprecated('1.4', 'The __init__() method on :class:`_reflection.Inspector` is deprecated and will be removed in a future release. Please use the :func:`.sqlalchemy.inspect` function on an :class:`_engine.Engine` or :class:`_engine.Connection` in order to acquire an :class:`_reflection.Inspector`.')
def __init__(self, bind):

Initialize a new _reflection.Inspector.

For a dialect-specific instance of _reflection.Inspector, see _reflection.Inspector.from_engine

Parameters
binda ~sqlalchemy.engine.Connectable, which is typically an instance of ~sqlalchemy.engine.Engine or ~sqlalchemy.engine.Connection.
def get_check_constraints(self, table_name, schema=None, **kw):

Return information about check constraints in table_name.

Given a string table_name and an optional string schema, return check constraint information as a list of dicts with these keys:

  • name - the check constraint's name

  • sqltext - the check constraint's SQL expression

  • dialect_options - may or may not be present; a dictionary with additional dialect-specific options for this CHECK constraint

    New in version 1.3.8.

New in version 1.1.0.
Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
def get_columns(self, table_name, schema=None, **kw):

Return information about columns in table_name.

Given a string table_name and an optional string schema, return column information as a list of dicts with these keys:

  • name - the column's name

  • type - the type of this column; an instance of ~sqlalchemy.types.TypeEngine

  • nullable - boolean flag if the column is NULL or NOT NULL

  • default - the column's server default value - this is returned as a string SQL expression.

  • autoincrement - indicates that the column is auto incremented - this is returned as a boolean or 'auto'

  • comment - (optional) the comment on the column. Only some dialects return this key

  • computed - (optional) when present it indicates that this column is computed by the database. Only some dialects return this key. Returned as a dict with the keys:

    • sqltext - the expression used to generate this column returned as a string SQL expression
    • persisted - (optional) boolean that indicates if the column is stored in the table

    New in version 1.3.16: - added support for computed reflection.

  • identity - (optional) when present it indicates that this column is a generated always column. Only some dialects return this key. For a list of keywords on this dict see _schema.Identity.

    New in version 1.4: - added support for identity column reflection.

  • dialect_options - (optional) a dict with dialect specific options

Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
Returns
list of dictionaries, each representing the definition of a database column.
def get_foreign_keys(self, table_name, schema=None, **kw):

Return information about foreign_keys in table_name.

Given a string table_name, and an optional string schema, return foreign key information as a list of dicts with these keys:

  • constrained_columns - a list of column names that make up the foreign key
  • referred_schema - the name of the referred schema
  • referred_table - the name of the referred table
  • referred_columns - a list of column names in the referred table that correspond to constrained_columns
  • name - optional name of the foreign key constraint.
Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
def get_indexes(self, table_name, schema=None, **kw):

Return information about indexes in table_name.

Given a string table_name and an optional string schema, return index information as a list of dicts with these keys:

  • name - the index's name

  • column_names - list of column names in order

  • unique - boolean

  • column_sorting - optional dict mapping column names to tuple of sort keywords, which may include asc, desc, nulls_first, nulls_last.

    New in version 1.3.5.

  • dialect_options - dict of dialect-specific index options. May not be present for all dialects.

    New in version 1.0.0.

Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
def get_pk_constraint(self, table_name, schema=None, **kw):

Return information about primary key constraint on table_name.

Given a string table_name, and an optional string schema, return primary key information as a dictionary with these keys:

  • constrained_columns - a list of column names that make up the primary key
  • name - optional name of the primary key constraint.
Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
def get_schema_names(self):
Return all schema names.
def get_sequence_names(self, schema=None):
Return all sequence names in schema.
Parameters
schemaOptional, retrieve names from a non-default schema. For special quoting, use .quoted_name.
def get_sorted_table_and_fkc_names(self, schema=None):

Return dependency-sorted table and foreign key constraint names in referred to within a particular schema.

This will yield 2-tuples of (tablename, [(tname, fkname), (tname, fkname), ...]) consisting of table names in CREATE order grouped with the foreign key constraint names that are not detected as belonging to a cycle. The final element will be (None, [(tname, fkname), (tname, fkname), ..]) which will consist of remaining foreign key constraint names that would require a separate CREATE step after-the-fact, based on dependencies between tables.

New in version 1.0.-.

See Also

_reflection.Inspector.get_table_names

.sort_tables_and_constraints - similar method which works with an already-given _schema.MetaData.

def get_table_comment(self, table_name, schema=None, **kw):

Return information about the table comment for table_name.

Given a string table_name and an optional string schema, return table comment information as a dictionary with these keys:

  • text -
    text of the comment.

Raises NotImplementedError for a dialect that does not support comments.

New in version 1.2.
def get_table_names(self, schema=None):

Return all table names in referred to within a particular schema.

The names are expected to be real tables only, not views. Views are instead returned using the _reflection.Inspector.get_view_names method.

See Also

_reflection.Inspector.get_sorted_table_and_fkc_names

_schema.MetaData.sorted_tables

Parameters
schemaSchema name. If schema is left at None, the database's default schema is used, else the named schema is searched. If the database does not support named schemas, behavior is undefined if schema is not passed as None. For special quoting, use .quoted_name.
def get_table_options(self, table_name, schema=None, **kw):

Return a dictionary of options specified when the table of the given name was created.

This currently includes some options that apply to MySQL tables.

Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
def get_temp_table_names(self):

Return a list of temporary table names for the current bind.

This method is unsupported by most dialects; currently only SQLite implements it.

New in version 1.0.0.
def get_temp_view_names(self):

Return a list of temporary view names for the current bind.

This method is unsupported by most dialects; currently only SQLite implements it.

New in version 1.0.0.
def get_unique_constraints(self, table_name, schema=None, **kw):

Return information about unique constraints in table_name.

Given a string table_name and an optional string schema, return unique constraint information as a list of dicts with these keys:

  • name - the unique constraint's name
  • column_names - list of column names in order
Parameters
table​_namestring name of the table. For special quoting, use .quoted_name.
schemastring schema name; if omitted, uses the default schema of the database connection. For special quoting, use .quoted_name.
**kwUndocumented
def get_view_definition(self, view_name, schema=None):
Return definition for view_name.
Parameters
view​_nameUndocumented
schemaOptional, retrieve names from a non-default schema. For special quoting, use .quoted_name.
def get_view_names(self, schema=None):
Return all view names in schema.
Parameters
schemaOptional, retrieve names from a non-default schema. For special quoting, use .quoted_name.
def has_sequence(self, sequence_name, schema=None):

Return True if the backend has a table of the given name.

New in version 1.4.
Parameters
sequence​_namename of the table to check
schemaschema name to query, if not the default schema.
def has_table(self, table_name, schema=None):

Return True if the backend has a table of the given name.

New in version 1.4: - the .Inspector.has_table method replaces the _engine.Engine.has_table method.
Parameters
table​_namename of the table to check
schemaschema name to query, if not the default schema.
def reflect_table(self, table, include_columns, exclude_columns=(), resolve_fks=True, _extend_on=None):

Given a _schema.Table object, load its internal constructs based on introspection.

This is the underlying method used by most dialects to produce table reflection. Direct usage is like:

from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy import inspect

engine = create_engine('...')
meta = MetaData()
user_table = Table('user', meta)
insp = inspect(engine)
insp.reflect_table(user_table, None)
Changed in version 1.4: Renamed from reflecttable to reflect_table
Parameters
tablea ~sqlalchemy.schema.Table instance.
include​_columnsa list of string column names to include in the reflection process. If None, all columns are reflected.
exclude​_columnsUndocumented
resolve​_fksUndocumented
​_extend​_onUndocumented
@util.deprecated_20(':meth:`_reflection.Inspector.reflecttable`', 'The :meth:`_reflection.Inspector.reflecttable` method was renamed to :meth:`_reflection.Inspector.reflect_table`. This deprecated alias will be removed in a future release.')
def reflecttable(self, *args, **kwargs):
See reflect_table. This method name is deprecated
@classmethod
def _construct(cls, init, bind):

Undocumented

@inspection._inspects(Connectable)
def _connectable_insp(bind):

Undocumented

@inspection._inspects(Connection)
def _connection_insp(bind):

Undocumented

@inspection._inspects(Engine)
def _engine_insp(bind):

Undocumented

def _init_connection(self, connection):

Undocumented

def _init_engine(self, engine):

Undocumented

def _init_legacy(self, bind):

Undocumented

@contextlib.contextmanager
def _inspection_context(self):
Return an _reflection.Inspector from this one that will run all operations on a single connection.
@contextlib.contextmanager
def _operation_context(self):

Return a context that optimizes for multiple operations on a single transaction.

This essentially allows connect()/close() to be called if we detected that we're against an _engine.Engine and not a _engine.Connection.

def _reflect_check_constraints(self, table_name, schema, table, cols_by_orig_name, include_columns, exclude_columns, reflection_options):

Undocumented

def _reflect_col_sequence(self, col_d, colargs):

Undocumented

def _reflect_column(self, table, col_d, include_columns, exclude_columns, cols_by_orig_name):

Undocumented

def _reflect_fk(self, table_name, schema, table, cols_by_orig_name, exclude_columns, resolve_fks, _extend_on, reflection_options):

Undocumented

def _reflect_indexes(self, table_name, schema, table, cols_by_orig_name, include_columns, exclude_columns, reflection_options):

Undocumented

def _reflect_pk(self, table_name, schema, table, cols_by_orig_name, exclude_columns):

Undocumented

def _reflect_table_comment(self, table_name, schema, table, reflection_options):

Undocumented

def _reflect_unique_constraints(self, table_name, schema, table, cols_by_orig_name, include_columns, exclude_columns, reflection_options):

Undocumented

_index_sort_exprs =

Undocumented

_op_context_requires_connect: bool =

Undocumented

bind =

Undocumented

dialect =

Undocumented

engine =

Undocumented

info_cache: dict =

Undocumented

@property
default_schema_name =

Return the default schema name presented by the dialect for the current engine's database user.

E.g. this is typically public for PostgreSQL and dbo for SQL Server.