class documentation

class Table(DialectKWArgs, SchemaItem, TableClause):

View In Hierarchy

Represent a table in a database.

e.g.:

mytable = Table("mytable", metadata,
                Column('mytable_id', Integer, primary_key=True),
                Column('value', String(50))
           )

The _schema.Table object constructs a unique instance of itself based on its name and optional schema name within the given _schema.MetaData object. Calling the _schema.Table constructor with the same name and same _schema.MetaData argument a second time will return the same _schema.Table object - in this way the _schema.Table constructor acts as a registry function.

See Also

:ref:`metadata_describing` - Introduction to database metadata

Constructor arguments are as follows:

Parameters
name

The name of this table as represented in the database.

The table name, along with the value of the schema parameter, forms a key which uniquely identifies this _schema.Table within the owning _schema.MetaData collection. Additional calls to _schema.Table with the same name, metadata, and schema name will return the same _schema.Table object.

Names which contain no upper case characters will be treated as case insensitive names, and will not be quoted unless they are a reserved word or contain special characters. A name with any number of upper case characters is considered to be case sensitive, and will be sent as quoted.

To enable unconditional quoting for the table name, specify the flag quote=True to the constructor, or use the .quoted_name construct to specify the name.

metadataa _schema.MetaData object which will contain this table. The metadata is used as a point of association of this table with other tables which are referenced via foreign key. It also may be used to associate this table with a particular .Connectable.
*argsAdditional positional arguments are used primarily to add the list of _schema.Column objects contained within this table. Similar to the style of a CREATE TABLE statement, other .SchemaItem constructs may be added here, including .PrimaryKeyConstraint, and _schema.ForeignKeyConstraint.
autoload

Defaults to False, unless :paramref:`_schema.Table.autoload_with` is set in which case it defaults to True; _schema.Column objects for this table should be reflected from the database, possibly augmenting objects that were explicitly specified. _schema.Column and other objects explicitly set on the table will replace corresponding reflected objects.

Deprecated since version 1.4: The autoload parameter is deprecated and will be removed in version 2.0. Please use the :paramref:`_schema.Table.autoload_with` parameter, passing an engine or connection.
autoload​_replace

Defaults to True; when using :paramref:`_schema.Table.autoload` in conjunction with :paramref:`_schema.Table.extend_existing`, indicates that _schema.Column objects present in the already-existing _schema.Table object should be replaced with columns of the same name retrieved from the autoload process. When False, columns already present under existing names will be omitted from the reflection process.

Note that this setting does not impact _schema.Column objects specified programmatically within the call to _schema.Table that also is autoloading; those _schema.Column objects will always replace existing columns of the same name when :paramref:`_schema.Table.extend_existing` is True.

autoload​_withAn _engine.Engine or _engine.Connection object, or a _reflection.Inspector object as returned by _sa.inspect against one, with which this _schema.Table object will be reflected. When set to a non-None value, the autoload process will take place for this table against the given engine or connection.
extend​_existing

When True, indicates that if this _schema.Table is already present in the given _schema.MetaData, apply further arguments within the constructor to the existing _schema.Table.

If :paramref:`_schema.Table.extend_existing` or :paramref:`_schema.Table.keep_existing` are not set, and the given name of the new _schema.Table refers to a _schema.Table that is already present in the target _schema.MetaData collection, and this _schema.Table specifies additional columns or other constructs or flags that modify the table's state, an error is raised. The purpose of these two mutually-exclusive flags is to specify what action should be taken when a _schema.Table is specified that matches an existing _schema.Table, yet specifies additional constructs.

:paramref:`_schema.Table.extend_existing` will also work in conjunction with :paramref:`_schema.Table.autoload` to run a new reflection operation against the database, even if a _schema.Table of the same name is already present in the target _schema.MetaData; newly reflected _schema.Column objects and other options will be added into the state of the _schema.Table, potentially overwriting existing columns and options of the same name.

As is always the case with :paramref:`_schema.Table.autoload`, _schema.Column objects can be specified in the same _schema.Table constructor, which will take precedence. Below, the existing table mytable will be augmented with _schema.Column objects both reflected from the database, as well as the given _schema.Column named "y":

Table("mytable", metadata,
            Column('y', Integer),
            extend_existing=True,
            autoload_with=engine
        )
implicit​_returningTrue by default - indicates that RETURNING can be used by default to fetch newly inserted primary key values, for backends which support this. Note that _sa.create_engine also provides an implicit_returning flag.
include​_columnsA list of strings indicating a subset of columns to be loaded via the autoload operation; table columns who aren't present in this list will not be represented on the resulting Table object. Defaults to None which indicates all columns should be reflected.
resolve​_fks

Whether or not to reflect _schema.Table objects related to this one via _schema.ForeignKey objects, when :paramref:`_schema.Table.autoload` or :paramref:`_schema.Table.autoload_with` is specified. Defaults to True. Set to False to disable reflection of related tables as _schema.ForeignKey objects are encountered; may be used either to save on SQL calls or to avoid issues with related tables that can't be accessed. Note that if a related table is already present in the _schema.MetaData collection, or becomes present later, a _schema.ForeignKey object associated with this _schema.Table will resolve to that table normally.

New in version 1.3.
infoOptional data dictionary which will be populated into the .SchemaItem.info attribute of this object.
keep​_existing

When True, indicates that if this Table is already present in the given _schema.MetaData, ignore further arguments within the constructor to the existing _schema.Table, and return the _schema.Table object as originally created. This is to allow a function that wishes to define a new _schema.Table on first call, but on subsequent calls will return the same _schema.Table, without any of the declarations (particularly constraints) being applied a second time.

If :paramref:`_schema.Table.extend_existing` or :paramref:`_schema.Table.keep_existing` are not set, and the given name of the new _schema.Table refers to a _schema.Table that is already present in the target _schema.MetaData collection, and this _schema.Table specifies additional columns or other constructs or flags that modify the table's state, an error is raised. The purpose of these two mutually-exclusive flags is to specify what action should be taken when a _schema.Table is specified that matches an existing _schema.Table, yet specifies additional constructs.

listeners

A list of tuples of the form (<eventname>, <fn>) which will be passed to .event.listen upon construction. This alternate hook to .event.listen allows the establishment of a listener function specific to this _schema.Table before the "autoload" process begins. Historically this has been intended for use with the .DDLEvents.column_reflect event, however note that this event hook may now be associated with the _schema.MetaData object directly:

def listen_for_reflect(table, column_info):
    "handle the column reflection event"
    # ...

t = Table(
    'sometable',
    autoload_with=engine,
    listeners=[
        ('column_reflect', listen_for_reflect)
    ])

See Also

_events.DDLEvents.column_reflect

must​_existWhen True, indicates that this Table must already be present in the given _schema.MetaData collection, else an exception is raised.
prefixesA list of strings to insert after CREATE in the CREATE TABLE statement. They will be separated by spaces.
quote

Force quoting of this table's name on or off, corresponding to True or False. When left at its default of None, the column identifier will be quoted according to whether the name is case sensitive (identifiers with at least one upper case character are treated as case sensitive), or if it's a reserved word. This flag is only needed to force quoting of a reserved word which is not known by the SQLAlchemy dialect.

Note

setting this flag to False will not provide case-insensitive behavior for table reflection; table reflection will always search for a mixed-case name in a case sensitive fashion. Case insensitive names are specified in SQLAlchemy only by stating the name with all lower case characters.

quote​_schemasame as 'quote' but applies to the schema identifier.
schema

The schema name for this table, which is required if the table resides in a schema other than the default selected schema for the engine's database connection. Defaults to None.

If the owning _schema.MetaData of this _schema.Table specifies its own :paramref:`_schema.MetaData.schema` parameter, then that schema name will be applied to this _schema.Table if the schema parameter here is set to None. To set a blank schema name on a _schema.Table that would otherwise use the schema set on the owning _schema.MetaData, specify the special symbol .BLANK_SCHEMA.

New in version 1.0.14: Added the .BLANK_SCHEMA symbol to allow a _schema.Table to have a blank schema name even when the parent _schema.MetaData specifies :paramref:`_schema.MetaData.schema`.

The quoting rules for the schema name are the same as those for the name parameter, in that quoting is applied for reserved words or case-sensitive names; to enable unconditional quoting for the schema name, specify the flag quote_schema=True to the constructor, or use the .quoted_name construct to specify the name.

comment

Optional string that will render an SQL comment on table creation.

New in version 1.2: Added the :paramref:`_schema.Table.comment` parameter to _schema.Table.
**kwAdditional 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.
Method __init__ Constructor for _schema.Table.
Method __new__ Undocumented
Method __repr__ Undocumented
Method __str__ Undocumented
Method ​_autoload Undocumented
Method ​_extra​_kwargs Undocumented
Method ​_gen​_cache​_key return an optional cache key.
Method ​_init Undocumented
Method ​_init​_collections Undocumented
Method ​_init​_existing Undocumented
Method ​_reset​_exported Undocumented
Method ​_set​_parent Associate with this SchemaEvent's parent object.
Method add​_is​_dependent​_on Add a 'dependency' for this Table.
Method append​_column Append a _schema.Column to this _schema.Table.
Method append​_constraint Append a _schema.Constraint to this _schema.Table.
Method create Issue a CREATE statement for this _schema.Table, using the given .Connectable for connectivity.
Method drop Issue a DROP statement for this _schema.Table, using the given .Connectable for connectivity.
Method exists Return True if this table exists.
Method to​_metadata Return a copy of this _schema.Table associated with a different _schema.MetaData.
Method tometadata Return a copy of this _schema.Table associated with a different _schema.MetaData.
Class Variable __visit​_name__ Undocumented
Class Variable ​_traverse​_internals Undocumented
Instance Variable ​_extra​_dependencies Undocumented
Instance Variable ​_prefixes Undocumented
Instance Variable comment Undocumented
Instance Variable constraints A collection of all _schema.Constraint objects associated with this _schema.Table.
Instance Variable foreign​_keys Return the collection of _schema.ForeignKey marker objects which this FromClause references.
Instance Variable fullname Undocumented
Instance Variable implicit​_returning _expression.TableClause doesn't support having a primary key or column -level defaults, so implicit returning doesn't apply.
Instance Variable indexes A collection of all _schema.Index objects associated with this _schema.Table.
Instance Variable info Info dictionary associated with the object, allowing user-defined data to be associated with this .SchemaItem.
Instance Variable metadata Undocumented
Instance Variable schema Define the 'schema' attribute for this _expression.FromClause.
Property ​_autoincrement​_column No PK or default support so no autoincrement column.
Property ​_sorted​_constraints Return the set of constraints as a list, sorted by creation order.
Property bind Return the connectable associated with this Table.
Property foreign​_key​_constraints _schema.ForeignKeyConstraint objects referred to by this _schema.Table.
Property key Return the 'key' for this _schema.Table.

Inherited from DialectKWArgs:

Class Method argument​_for Add a new kind of dialect-specific keyword argument for this class.
Method ​_kw​_reg​_for​_dialect​_cls Undocumented
Method ​_validate​_dialect​_kwargs Undocumented
Class Variable ​_dialect​_kwargs​_traverse​_internals Undocumented
Class Variable ​_kw​_registry Undocumented
Property dialect​_kwargs A collection of keyword arguments specified as dialect-specific options to this construct.
Property dialect​_options A collection of keyword arguments specified as dialect-specific options to this construct.
Property kwargs A synonym for .DialectKWArgs.dialect_kwargs.

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​_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

Inherited from TableClause:

Method delete Generate a _expression.delete construct against this _expression.TableClause.
Method insert Generate an _expression.insert construct against this _expression.TableClause.
Method update Generate an _expression.update construct against this _expression.TableClause.
Instance Variable primary​_key Return the iterable collection of _schema.Column objects which comprise the primary key of this _selectable.FromClause.
Method ​_refresh​_for​_new​_column Given a column added to the .c collection of an underlying selectable, produce the local version of that column, assuming this selectable ultimately should proxy this column.
Class Variable named​_with​_column Undocumented
Instance Variable ​_columns Undocumented
Instance Variable name Undocumented
Property ​_from​_objects Undocumented
Property description A brief description of this _expression.FromClause.

Inherited from DMLTableRole (via TableClause):

Class Variable ​_role​_name Undocumented

Inherited from FromClauseRole (via TableClause, DMLTableRole):

Class Variable ​_is​_subquery Undocumented
Property ​_hide​_froms Undocumented

Inherited from ColumnsClauseRole (via TableClause, DMLTableRole, FromClauseRole):

Property ​_select​_iterable Undocumented

Inherited from AllowsLambdaRole (via TableClause, DMLTableRole, FromClauseRole, ColumnsClauseRole):

Class Variable allows​_lambda Undocumented

Inherited from UsesInspection (via TableClause, DMLTableRole, FromClauseRole, ColumnsClauseRole):

Class Variable ​_post​_inspect Undocumented
Class Variable uses​_inspection Undocumented

Inherited from SQLRole (via TableClause, DMLTableRole, FromClauseRole, ColumnsClauseRole, ColumnListRole):

Class Variable allows​_lambda Undocumented
Class Variable uses​_inspection Undocumented

Inherited from AllowsLambdaRole (via TableClause, DMLTableRole, FromClauseRole, JoinTargetRole):

Class Variable allows​_lambda Undocumented

Inherited from UsesInspection (via TableClause, DMLTableRole, FromClauseRole, JoinTargetRole):

Class Variable ​_post​_inspect Undocumented
Class Variable uses​_inspection Undocumented

Inherited from SQLRole (via TableClause, DMLTableRole, FromClauseRole, JoinTargetRole, StructuralRole):

Class Variable allows​_lambda Undocumented
Class Variable uses​_inspection Undocumented

Inherited from Immutable (via TableClause):

Method ​_clone Undocumented
Method ​_copy​_internals Undocumented
Method params Undocumented
Method unique​_params Undocumented
Class Variable ​_is​_immutable Undocumented

Inherited from FromClause (via TableClause):

Method alias Return an alias of this _expression.FromClause.
Method is​_derived​_from Return True if this _expression.FromClause is 'derived' from the given FromClause.
Method join Return a _expression.Join from this _expression.FromClause to another FromClause.
Method outerjoin No summary
Method select Return a SELECT of this _expression.FromClause.
Method table​_valued Return a _sql.TableValuedColumn object for this _expression.FromClause.
Method tablesample Return a TABLESAMPLE alias of this _expression.FromClause.
Class Variable c Undocumented
Method ​_anonymous​_fromclause Undocumented
Method ​_generate​_fromclause​_column​_proxies Undocumented
Method ​_is​_lexical​_equivalent Return True if this _expression.FromClause and the other represent the same lexical identity.
Method ​_populate​_column​_collection Called on subclasses to establish the .c collection.
Method ​_reset​_column​_collection Reset the attributes linked to the FromClause.c attribute.
Class Variable ​_hide​_froms Undocumented
Class Variable ​_is​_from​_clause Undocumented
Class Variable ​_is​_join Undocumented
Class Variable ​_select​_iterable Undocumented
Class Variable ​_use​_schema​_map Undocumented
Class Variable is​_selectable Undocumented
Property ​_cols​_populated Undocumented
Property columns A named-based collection of _expression.ColumnElement objects maintained by this _expression.FromClause.
Property entity​_namespace Return a namespace used for name-based access in SQL expressions.
Property exported​_columns A _expression.ColumnCollection that represents the "exported" columns of this _expression.Selectable.

Inherited from FromClauseRole (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole):

Class Variable ​_is​_subquery Undocumented
Class Variable ​_role​_name Undocumented

Inherited from AllowsLambdaRole (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole, FromClauseRole, ColumnsClauseRole):

Class Variable allows​_lambda Undocumented

Inherited from UsesInspection (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole, FromClauseRole, ColumnsClauseRole):

Class Variable ​_post​_inspect Undocumented
Class Variable uses​_inspection Undocumented

Inherited from SQLRole (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole, FromClauseRole, ColumnsClauseRole, ColumnListRole):

Class Variable allows​_lambda Undocumented
Class Variable uses​_inspection Undocumented

Inherited from AllowsLambdaRole (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole, FromClauseRole, JoinTargetRole):

Class Variable allows​_lambda Undocumented

Inherited from UsesInspection (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole, FromClauseRole, JoinTargetRole):

Class Variable ​_post​_inspect Undocumented
Class Variable uses​_inspection Undocumented

Inherited from SQLRole (via TableClause, FromClause, AnonymizedFromClauseRole, StrictFromClauseRole, FromClauseRole, JoinTargetRole, StructuralRole):

Class Variable allows​_lambda Undocumented
Class Variable uses​_inspection Undocumented

Inherited from Selectable (via TableClause, FromClause):

Method corresponding​_column No summary
Method lateral Return a LATERAL alias of this _expression.Selectable.
Method replace​_selectable Replace all occurrences of _expression.FromClause 'old' with the given _expression.Alias object, returning a copy of this _expression.FromClause.

Inherited from ReturnsRows (via TableClause, FromClause, Selectable):

Class Variable ​_is​_lateral Undocumented
Class Variable ​_is​_returns​_rows Undocumented
Class Variable ​_is​_select​_statement Undocumented
Property ​_all​_selected​_columns A sequence of column expression objects that represents the "selected" columns of this _expression.ReturnsRows.
Property selectable Undocumented

Inherited from ReturnsRowsRole (via TableClause, FromClause, Selectable, ReturnsRows):

Class Variable ​_role​_name Undocumented

Inherited from SQLRole (via TableClause, FromClause, Selectable, ReturnsRows, ReturnsRowsRole):

Class Variable allows​_lambda Undocumented
Class Variable uses​_inspection Undocumented

Inherited from ClauseElement (via TableClause, FromClause, Selectable, ReturnsRows):

Method compare Compare this _expression.ClauseElement to the given _expression.ClauseElement.
Method compile Compile this SQL expression.
Method params Return a copy with _expression.bindparam elements replaced.
Method self​_group Apply a 'grouping' to this _expression.ClauseElement.
Method unique​_params Return a copy with _expression.bindparam elements replaced.
Method __bool__ Undocumented
Method __getstate__ Undocumented
Method __invert__ Undocumented
Method ​_clone Create a shallow copy of this ClauseElement.
Method ​_cloned​_set Return the set consisting all cloned ancestors of this ClauseElement.
Method ​_compile​_w​_cache Undocumented
Method ​_compiler Return a compiler appropriate for this ClauseElement, given a Dialect.
Method ​_execute​_on​_connection Undocumented
Method ​_negate Undocumented
Method ​_negate​_in​_binary a hook to allow the right side of a binary expression to respond to a negation of the binary expression.
Method ​_replace​_params Undocumented
Method ​_set​_propagate​_attrs Undocumented
Method ​_ungroup Return this _expression.ClauseElement without any groupings.
Method ​_with​_binary​_element​_type in the context of binary expression, convert the type of this object to the one given.
Class Variable ​_cache​_key​_traversal Undocumented
Class Variable ​_is​_bind​_parameter Undocumented
Class Variable ​_is​_clause​_list Undocumented
Class Variable ​_is​_clone​_of Undocumented
Class Variable ​_is​_from​_container Undocumented
Class Variable ​_is​_immutable Undocumented
Class Variable ​_is​_lambda​_element Undocumented
Class Variable ​_is​_select​_container Undocumented
Class Variable ​_is​_singleton​_constant Undocumented
Class Variable ​_is​_text​_clause Undocumented
Class Variable ​_is​_textual Undocumented
Class Variable ​_order​_by​_label​_element Undocumented
Class Variable is​_clause​_element Undocumented
Class Variable stringify​_dialect Undocumented
Class Variable supports​_execution Undocumented
Instance Variable ​_propagate​_attrs like annotations, however these propagate outwards liberally as SQL constructs are built, and are set up at construction time.
Property ​_constructor return the 'constructor' for this ClauseElement.

Inherited from SQLRole (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement):

Class Variable allows​_lambda Undocumented
Class Variable uses​_inspection Undocumented

Inherited from SupportsWrappingAnnotations (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement):

Method ​_annotate return a copy of this ClauseElement with annotations updated by the given dictionary.
Method ​_deannotate return a copy of this _expression.ClauseElement with annotations removed.
Method ​_with​_annotations return a copy of this ClauseElement with annotations replaced by the given dictionary.

Inherited from SupportsAnnotations (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement, SupportsWrappingAnnotations):

Property ​_annotations​_cache​_key Undocumented

Inherited from MemoizedHasCacheKey (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement):

Method ​_generate​_cache​_key return a cache key.

Inherited from HasCacheKey (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement, MemoizedHasCacheKey):

Class Variable inherit​_cache Indicate if this .HasCacheKey instance should make use of the cache key generation scheme used by its immediate superclass.
Class Method ​_generate​_cache​_attrs generate cache key dispatcher for a new class.
Class Method ​_generate​_cache​_key​_for​_object Undocumented
Class Variable __slots__ Undocumented
Class Variable ​_hierarchy​_supports​_caching private attribute which may be set to False to prevent the inherit_cache warning from being emitted for a hierarchy of subclasses.
Class Variable ​_is​_has​_cache​_key Undocumented

Inherited from HasCopyInternals (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement):

Method ​_copy​_internals Reassign internal elements to be clones of themselves.

Inherited from Traversible (via TableClause, FromClause, Selectable, ReturnsRows, ClauseElement):

Method get​_children Return immediate child .visitors.Traversible elements of this .visitors.Traversible.
Method __class​_getitem__ Undocumented
def __init__(self, *args, **kw):

Constructor for _schema.Table.

This method is a no-op. See the top-level documentation for _schema.Table for constructor arguments.

@util.deprecated_params(mustexist=('1.4', 'Deprecated alias of :paramref:`_schema.Table.must_exist`'), autoload=('2.0', 'The autoload parameter is deprecated and will be removed in version 2.0. Please use the autoload_with parameter, passing an engine or connection.'))
def __new__(cls, *args, **kw):

Undocumented

def __repr__(self):
def __str__(self):
def _autoload(self, metadata, autoload_with, include_columns, exclude_columns=(), resolve_fks=True, _extend_on=None):

Undocumented

def _extra_kwargs(self, **kwargs):

Undocumented

def _gen_cache_key(self, anon_map, bindparams):

return an optional cache key.

The cache key is a tuple which can contain any series of objects that are hashable and also identifies this object uniquely within the presence of a larger SQL expression or statement, for the purposes of caching the resulting query.

The cache key should be based on the SQL compiled structure that would ultimately be produced. That is, two structures that are composed in exactly the same way should produce the same cache key; any difference in the structures that would affect the SQL string or the type handlers should result in a different cache key.

If a structure cannot produce a useful cache key, the NO_CACHE symbol should be added to the anon_map and the method should return None.

def _init(self, name, metadata, *args, **kwargs):

Undocumented

def _init_collections(self):
def _init_existing(self, *args, **kwargs):

Undocumented

def _reset_exported(self):

Undocumented

def _set_parent(self, metadata, **kw):
Associate with this SchemaEvent's parent object.
def add_is_dependent_on(self, table):

Add a 'dependency' for this Table.

This is another Table object which must be created first before this one can, or dropped after this one.

Usually, dependencies between tables are determined via ForeignKey objects. However, for other situations that create dependencies outside of foreign keys (rules, inheriting), this method can manually establish such a link.

def append_column(self, column, replace_existing=False):

Append a _schema.Column to this _schema.Table.

The "key" of the newly added _schema.Column, i.e. the value of its .key attribute, will then be available in the .c collection of this _schema.Table, and the column definition will be included in any CREATE TABLE, SELECT, UPDATE, etc. statements generated from this _schema.Table construct.

Note that this does not change the definition of the table as it exists within any underlying database, assuming that table has already been created in the database. Relational databases support the addition of columns to existing tables using the SQL ALTER command, which would need to be emitted for an already-existing table that doesn't contain the newly added column.

Parameters
columnUndocumented
replace​_existing

When True, allows replacing existing columns. When False, the default, an warning will be raised if a column with the same .key already exists. A future version of sqlalchemy will instead rise a warning.

New in version 1.4.0.
def append_constraint(self, constraint):

Append a _schema.Constraint to this _schema.Table.

This has the effect of the constraint being included in any future CREATE TABLE statement, assuming specific DDL creation events have not been associated with the given _schema.Constraint object.

Note that this does not produce the constraint within the relational database automatically, for a table that already exists in the database. To add a constraint to an existing relational database table, the SQL ALTER command must be used. SQLAlchemy also provides the .AddConstraint construct which can produce this SQL when invoked as an executable clause.

def create(self, bind=None, checkfirst=False):

Issue a CREATE statement for this _schema.Table, using the given .Connectable for connectivity.

Note

the "bind" argument will be required in SQLAlchemy 2.0.

See Also

_schema.MetaData.create_all.

def drop(self, bind=None, checkfirst=False):

Issue a DROP statement for this _schema.Table, using the given .Connectable for connectivity.

Note

the "bind" argument will be required in SQLAlchemy 2.0.

See Also

_schema.MetaData.drop_all.

@util.deprecated('1.4', 'The :meth:`_schema.Table.exists` method is deprecated and will be removed in a future release. Please refer to :meth:`_reflection.Inspector.has_table`.')
def exists(self, bind=None):
Return True if this table exists.
def to_metadata(self, metadata, schema=RETAIN_SCHEMA, referred_schema_fn=None, name=None):

Return a copy of this _schema.Table associated with a different _schema.MetaData.

E.g.:

m1 = MetaData()

user = Table('user', m1, Column('id', Integer, primary_key=True))

m2 = MetaData()
user_copy = user.to_metadata(m2)
Changed in version 1.4: The _schema.Table.to_metadata function was renamed from _schema.Table.tometadata.
Parameters
metadataTarget _schema.MetaData object, into which the new _schema.Table object will be created.
schema

optional string name indicating the target schema. Defaults to the special symbol .RETAIN_SCHEMA which indicates that no change to the schema name should be made in the new _schema.Table. If set to a string name, the new _schema.Table will have this new name as the .schema. If set to None, the schema will be set to that of the schema set on the target _schema.MetaData, which is typically None as well, unless set explicitly:

m2 = MetaData(schema='newschema')

# user_copy_one will have "newschema" as the schema name
user_copy_one = user.to_metadata(m2, schema=None)

m3 = MetaData()  # schema defaults to None

# user_copy_two will have None as the schema name
user_copy_two = user.to_metadata(m3, schema=None)
referred​_schema​_fn

optional callable which can be supplied in order to provide for the schema name that should be assigned to the referenced table of a _schema.ForeignKeyConstraint. The callable accepts this parent _schema.Table, the target schema that we are changing to, the _schema.ForeignKeyConstraint object, and the existing "target schema" of that constraint. The function should return the string schema name that should be applied. E.g.:

def referred_schema_fn(table, to_schema,
                                constraint, referred_schema):
    if referred_schema == 'base_tables':
        return referred_schema
    else:
        return to_schema

new_table = table.to_metadata(m2, schema="alt_schema",
                        referred_schema_fn=referred_schema_fn)
New in version 0.9.2.
name

optional string name indicating the target table name. If not specified or None, the table name is retained. This allows a _schema.Table to be copied to the same _schema.MetaData target with a new name.

New in version 1.0.0.
@util.deprecated('1.4', ':meth:`_schema.Table.tometadata` is renamed to :meth:`_schema.Table.to_metadata`')
def tometadata(self, metadata, schema=RETAIN_SCHEMA, referred_schema_fn=None, name=None):

Return a copy of this _schema.Table associated with a different _schema.MetaData.

See _schema.Table.to_metadata for a full description.

__visit_name__: str =
_traverse_internals =
_extra_dependencies: set =

Undocumented

_prefixes =

Undocumented

comment =

Undocumented

constraints: set =

A collection of all _schema.Constraint objects associated with this _schema.Table.

Includes _schema.PrimaryKeyConstraint, _schema.ForeignKeyConstraint, _schema.UniqueConstraint, _schema.CheckConstraint. A separate collection _schema.Table.foreign_key_constraints refers to the collection of all _schema.ForeignKeyConstraint objects, and the _schema.Table.primary_key attribute refers to the single _schema.PrimaryKeyConstraint associated with the _schema.Table.

See Also

_schema.Table.constraints

_schema.Table.primary_key

_schema.Table.foreign_key_constraints

_schema.Table.indexes

_reflection.Inspector

foreign_keys: set =

Return the collection of _schema.ForeignKey marker objects which this FromClause references.

Each _schema.ForeignKey is a member of a _schema.Table-wide _schema.ForeignKeyConstraint.

See Also

_schema.Table.foreign_key_constraints

fullname =
implicit_returning =
_expression.TableClause doesn't support having a primary key or column -level defaults, so implicit returning doesn't apply.
indexes: set =

A collection of all _schema.Index objects associated with this _schema.Table.

See Also

_reflection.Inspector.get_indexes

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.

metadata =

Undocumented

schema =

Define the 'schema' attribute for this _expression.FromClause.

This is typically None for most objects except that of _schema.Table, where it is taken as the value of the :paramref:`_schema.Table.schema` argument.

@property
_autoincrement_column =
No PK or default support so no autoincrement column.
@property
_sorted_constraints =
Return the set of constraints as a list, sorted by creation order.
@property
bind =
Return the connectable associated with this Table.
@property
foreign_key_constraints =

_schema.ForeignKeyConstraint objects referred to by this _schema.Table.

This list is produced from the collection of _schema.ForeignKey objects currently associated.

See Also

_schema.Table.constraints

_schema.Table.foreign_keys

_schema.Table.indexes

@property
key =

Return the 'key' for this _schema.Table.

This value is used as the dictionary key within the _schema.MetaData.tables collection. It is typically the same as that of _schema.Table.name for a table with no _schema.Table.schema set; otherwise it is typically of the form schemaname.tablename.