class MetaData(SchemaItem):
Known subclasses: sqlalchemy.sql.schema.ThreadLocalMetaData
A collection of _schema.Table
objects and their associated schema
constructs.
Holds a collection of _schema.Table
objects as well as
an optional binding to an _engine.Engine
or
_engine.Connection
. If bound, the _schema.Table
objects
in the collection and their columns may participate in implicit SQL
execution.
The _schema.Table
objects themselves are stored in the
_schema.MetaData.tables
dictionary.
_schema.MetaData
is a thread-safe object for read operations.
Construction of new tables within a single _schema.MetaData
object,
either explicitly or via reflection, may not be completely thread-safe.
See Also
:ref:`metadata_describing` - Introduction to database metadata
Method | __contains__ |
Undocumented |
Method | __getstate__ |
Undocumented |
Method | __init__ |
Create a new MetaData object. |
Method | __repr__ |
Undocumented |
Method | __setstate__ |
Undocumented |
Method | _add_table |
Undocumented |
Method | _bind_to |
Bind this MetaData to an Engine, Connection, string or URL. |
Method | _remove_table |
Undocumented |
Method | bind |
An _engine.Engine or _engine.Connection to which this _schema.MetaData is bound. |
Method | clear |
Clear all Table objects from this MetaData. |
Method | create_all |
Create all tables stored in this metadata. |
Method | drop_all |
Drop all tables stored in this metadata. |
Method | is_bound |
True if this MetaData is bound to an Engine or Connection. |
Method | reflect |
Load all available table definitions from the database. |
Method | remove |
Remove the given Table object from this MetaData. |
Class Variable | __visit_name__ |
Undocumented |
Instance Variable | _bind |
Undocumented |
Instance Variable | _fk_memos |
Undocumented |
Instance Variable | _schemas |
Undocumented |
Instance Variable | _sequences |
Undocumented |
Instance Variable | info |
Info dictionary associated with the object, allowing user-defined data to be associated with this .SchemaItem . |
Instance Variable | naming_convention |
Undocumented |
Instance Variable | schema |
Undocumented |
Instance Variable | tables |
A dictionary of _schema.Table objects keyed to their name or "table key". |
Property | sorted_tables |
Returns a list of _schema.Table objects sorted in order of foreign key dependency. |
Inherited from SchemaItem
:
Method | _init_items |
Initialize the list of child items for this SchemaItem. |
Method | _schema_item_copy |
Undocumented |
Class Variable | _use_schema_map |
Undocumented |
Class Variable | create_drop_stringify_dialect |
Undocumented |
Inherited from SchemaEventTarget
(via SchemaItem
):
Method | _set_parent |
Associate with this SchemaEvent's parent object. |
Method | _set_parent_with_dispatch |
Undocumented |
Inherited from Traversible
(via SchemaItem
):
Method | get_children |
Return immediate child .visitors.Traversible elements of this .visitors.Traversible . |
Method | __class_getitem__ |
Undocumented |
sqlalchemy.sql.schema.ThreadLocalMetaData
Parameters | |
bind | An Engine or Connection to bind to. May also be a string or URL
instance, these are passed to _sa.create_engine and
this _schema.MetaData will
be bound to the resulting engine. |
schema | The default schema to use for the See Also :ref:`schema_metadata_schema_name` - details on how the :paramref:`_schema.MetaData.schema` parameter is used. |
quote_schema | Sets the quote_schema flag for those _schema.Table ,
.Sequence , and other objects which make usage of the
local schema name. |
naming_convention | a dictionary referring to values which
will establish default naming conventions for The keys of this dictionary may be:
The values associated with each "constraint class" or "constraint
mnemonic" key are string naming templates, such as
"uq_%(table_name)s_%(column_0_name)s",
which describe how the name should be composed. The values
associated with user-defined "token" keys should be callables of the
form fn(constraint, table), which accepts the constraint/index
object and The built-in names are as follows, some of which may only be available for certain types of constraint:
New in version 1.3.0: - added new %(column_0N_name)s,
%(column_0_N_name)s, and related tokens that produce
concatenations of names, keys, or labels for all columns referred
to by a given constraint.
See Also :ref:`constraint_naming_conventions` - for detailed usage examples. |
info | Optional data dictionary which will be populated into the
New in version 1.0.0.
|
sqlalchemy.sql.schema.ThreadLocalMetaData
sqlalchemy.sql.schema.ThreadLocalMetaData
An _engine.Engine
or _engine.Connection
to which this
_schema.MetaData
is bound.
Typically, a _engine.Engine
is assigned to this attribute
so that "implicit execution" may be used, or alternatively
as a means of providing engine binding information to an
ORM .Session
object:
engine = create_engine("someurl://") metadata.bind = engine
See Also
:ref:`dbengine_implicit` - background on "bound metadata"
Create all tables stored in this metadata.
Conditional by default, will not attempt to recreate tables already present in the target database.
Parameters | |
bind | A Note the "bind" argument will be required in SQLAlchemy 2.0. |
tables | Optional list of Table objects, which is a subset of the total tables in the MetaData (others are ignored). |
checkfirst | Defaults to True, don't issue CREATEs for tables already present in the target database. |
Drop all tables stored in this metadata.
Conditional by default, will not attempt to drop tables not present in the target database.
Parameters | |
bind | A Note the "bind" argument will be required in SQLAlchemy 2.0. |
tables | Optional list of Table objects, which is a subset of the total tables in the MetaData (others are ignored). |
checkfirst | Defaults to True, only issue DROPs for tables confirmed to be present in the target database. |
sqlalchemy.sql.schema.ThreadLocalMetaData
Load all available table definitions from the database.
Automatically creates Table entries in this MetaData for any table available in the database but not yet present in the MetaData. May be called multiple times to pick up tables recently added to the database, however no special action is taken if a table in this MetaData no longer exists in the database.
Parameters | |
bind | A Note the "bind" argument will be required in SQLAlchemy 2.0. |
schema | Optional, query and reflect tables from an alternate schema.
If None, the schema associated with this _schema.MetaData
is used, if any. |
views | If True, also reflect views. |
only | Optional. Load only a sub-set of available named tables. May be specified as a sequence of names or a callable. If a sequence of names is provided, only those tables will be reflected. An error is raised if a table is requested but not available. Named tables already present in this MetaData are ignored. If a callable is provided, it will be used as a boolean predicate to filter the list of potential table names. The callable is called with a table name and this MetaData instance as positional arguments and should return a true value for any table to reflect. |
extend_existing | Passed along to each
New in version 0.9.1.
|
autoload_replace | Passed along to each
New in version 0.9.1.
|
resolve_fks | if True, reflect
New in version 1.3.0.
|
**dialect_kwargs | Additional keyword arguments not mentioned above are dialect specific, and passed in the form <dialectname>_<argname>. See the documentation regarding an individual dialect at :ref:`dialect_toplevel` for detail on documented arguments.
|
sqlalchemy.sql.schema.SchemaItem.info
Info dictionary associated with the object, allowing user-defined
data to be associated with this .SchemaItem
.
The dictionary is automatically generated when first accessed.
It can also be specified in the constructor of some objects,
such as _schema.Table
and _schema.Column
.
A dictionary of _schema.Table
objects keyed to their name or "table key".
The exact key is that determined by the _schema.Table.key
attribute;
for a table with no _schema.Table.schema
attribute,
this is the same
as _schema.Table.name
. For a table with a schema,
it is typically of the
form schemaname.tablename.
See Also
_schema.MetaData.sorted_tables
Returns a list of _schema.Table
objects sorted in order of
foreign key dependency.
The sorting will place _schema.Table
objects that have dependencies
first, before the dependencies themselves, representing the
order in which they can be created. To get the order in which
the tables would be dropped, use the reversed() Python built-in.
Warning
The .MetaData.sorted_tables
attribute cannot by itself
accommodate automatic resolution of dependency cycles between
tables, which are usually caused by mutually dependent foreign key
constraints. When these cycles are detected, the foreign keys
of these tables are omitted from consideration in the sort.
A warning is emitted when this condition occurs, which will be an
exception raise in a future release. Tables which are not part
of the cycle will still be returned in dependency order.
To resolve these cycles, the
:paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be
applied to those constraints which create a cycle. Alternatively,
the _schema.sort_tables_and_constraints
function will
automatically return foreign key constraints in a separate
collection when cycles are detected so that they may be applied
to a schema separately.
.MetaData.sorted_tables
cannot perform a proper sort
due to cyclical dependencies. This will be an exception in a
future release. Additionally, the sort will continue to return
other tables not involved in the cycle in dependency order which
was not the case previously.See Also
_schema.sort_tables
_schema.sort_tables_and_constraints
_schema.MetaData.tables
_reflection.Inspector.get_table_names
_reflection.Inspector.get_sorted_table_and_fkc_names