# schema.py
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
"""The schema module provides the building blocks for database metadata.
Each element within this module describes a database entity which can be
created and dropped, or is otherwise part of such an entity. Examples include
tables, columns, sequences, and indexes.
All entities are subclasses of :class:`~sqlalchemy.schema.SchemaItem`, and as defined
in this module they are intended to be agnostic of any vendor-specific
constructs.
A collection of entities are grouped into a unit called
:class:`~sqlalchemy.schema.MetaData`. MetaData serves as a logical grouping of schema
elements, and can also be associated with an actual database connection such
that operations involving the contained elements can contact the database as
needed.
Two of the elements here also build upon their "syntactic" counterparts, which
are defined in :class:`~sqlalchemy.sql.expression.`, specifically
:class:`~sqlalchemy.schema.Table` and :class:`~sqlalchemy.schema.Column`. Since these objects
are part of the SQL expression language, they are usable as components in SQL
expressions.
"""
import re, inspect
from sqlalchemy import types, exc, util, databases
from sqlalchemy.sql import expression, visitors
URL = None
__all__ = ['SchemaItem', 'Table', 'Column', 'ForeignKey', 'Sequence', 'Index',
'ForeignKeyConstraint', 'PrimaryKeyConstraint', 'CheckConstraint',
'UniqueConstraint', 'DefaultGenerator', 'Constraint', 'MetaData',
'ThreadLocalMetaData', 'SchemaVisitor', 'PassiveDefault',
'DefaultClause', 'FetchedValue', 'ColumnDefault', 'DDL']
__all__.sort()
class SchemaItem(visitors.Visitable):
"""Base class for items that define a database schema."""
__visit_name__ = 'schema_item'
quote = None
def _init_items(self, *args):
"""Initialize the list of child items for this SchemaItem."""
for item in args:
if item is not None:
item._set_parent(self)
def _set_parent(self, parent):
"""Associate with this SchemaItem's parent object."""
raise NotImplementedError()
def get_children(self, **kwargs):
"""used to allow SchemaVisitor access"""
return []
def __repr__(self):
return "%s()" % self.__class__.__name__
@property
def bind(self):
"""Return the connectable associated with this SchemaItem."""
m = self.metadata
return m and m.bind or None
@property
def info(self):
try:
return self._info
except AttributeError:
self._info = {}
return self._info
def _get_table_key(name, schema):
if schema is None:
return name
else:
return schema + "." + name
class _TableSingleton(visitors.VisitableType):
"""A metaclass used by the ``Table`` object to provide singleton behavior."""
def __call__(self, name, metadata, *args, **kwargs):
schema = kwargs.get('schema', kwargs.get('owner', None))
useexisting = kwargs.pop('useexisting', False)
mustexist = kwargs.pop('mustexist', False)
key = _get_table_key(name, schema)
try:
table = metadata.tables[key]
if not useexisting and table._cant_override(*args, **kwargs):
raise exc.InvalidRequestError(
"Table '%s' is already defined for this MetaData instance. "
"Specify 'useexisting=True' to redefine options and "
"columns on an existing Table object." % key)
else:
table._init_existing(*args, **kwargs)
return table
except KeyError:
if mustexist:
raise exc.InvalidRequestError(
"Table '%s' not defined" % (key))
try:
return type.__call__(self, name, metadata, *args, **kwargs)
except:
if key in metadata.tables:
del metadata.tables[key]
raise
class Table(SchemaItem, expression.TableClause):
"""Represent a table in a database."""
__metaclass__ = _TableSingleton
__visit_name__ = 'table'
ddl_events = ('before-create', 'after-create', 'before-drop', 'after-drop')
def __init__(self, name, metadata, *args, **kwargs):
"""
Construct a Table.
:param name: The name of this table as represented in the database.
This property, along with the *schema*, indicates the *singleton
identity* of this table in relation to its parent :class:`MetaData`.
Additional calls to :class:`Table` with the same name, metadata,
and schema name will return the same :class:`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. Names with any number of upper
case characters will be quoted and sent exactly. Note that this
behavior applies even for databases which standardize upper
case names as case insensitive such as Oracle.
:param metadata: a :class:`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
:class:`~sqlalchemy.engine.base.Connectable`.
:param \*args: Additional positional arguments are used primarily
to add the list of :class:`Column` objects contained within this table.
Similar to the style of a CREATE TABLE statement, other :class:`SchemaItem`
constructs may be added here, including :class:`PrimaryKeyConstraint`,
and :class:`ForeignKeyConstraint`.
:param autoload: Defaults to False: the Columns for this table should be reflected
from the database. Usually there will be no Column objects in the
constructor if this property is set.
:param autoload_with: If autoload==True, this is an optional Engine or Connection
instance to be used for the table reflection. If ``None``, the
underlying MetaData's bound connectable will be used.
:param include_columns: A 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.
:param info: A dictionary which defaults to ``{}``. A space to store application
specific data. This must be a dictionary.
:param mustexist: When ``True``, indicates that this Table must already
be present in the given :class:`MetaData`` collection.
:param prefixes:
A list of strings to insert after CREATE in the CREATE TABLE
statement. They will be separated by spaces.
:param 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.
:param quote_schema: same as 'quote' but applies to the schema identifier.
:param 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``.
:param useexisting: When ``True``, indicates that if this Table is already
present in the given :class:`MetaData`, apply further arguments within
the constructor to the existing :class:`Table`. If this flag is not
set, an error is raised when the parameters of an existing :class:`Table`
are overwritten.
"""
super(Table, self).__init__(name)
self.metadata = metadata
self.schema = kwargs.pop('schema', kwargs.pop('owner', None))
self.indexes = set()
self.constraints = set()
self._columns = expression.ColumnCollection()
self.primary_key = PrimaryKeyConstraint()
self._foreign_keys = util.OrderedSet()
self.ddl_listeners = util.defaultdict(list)
self.kwargs = {}
if self.schema is not None:
self.fullname = "%s.%s" % (self.schema, self.name)
else:
self.fullname = self.name
autoload = kwargs.pop('autoload', False)
autoload_with = kwargs.pop('autoload_with', None)
include_columns = kwargs.pop('include_columns', None)
self._set_parent(metadata)
self.quote = kwargs.pop('quote', None)
self.quote_schema = kwargs.pop('quote_schema', None)
if kwargs.get('info'):
self._info = kwargs.pop('info')
self._prefixes = kwargs.pop('prefixes', [])
self.__extra_kwargs(**kwargs)
# load column definitions from the database if 'autoload' is defined
# we do it after the table is in the singleton dictionary to support
# circular foreign keys
if autoload:
if autoload_with:
autoload_with.reflecttable(self, include_columns=include_columns)
else:
_bind_or_error(metadata).reflecttable(self, include_columns=include_columns)
# initialize all the column, etc. objects. done after reflection to
# allow user-overrides
self.__post_init(*args, **kwargs)
def _init_existing(self, *args, **kwargs):
autoload = kwargs.pop('autoload', False)
autoload_with = kwargs.pop('autoload_with', None)
schema = kwargs.pop('schema', None)
if schema and schema != self.schema:
raise exc.ArgumentError(
"Can't change schema of existing table from '%s' to '%s'",
(self.schema, schema))
include_columns = kwargs.pop('include_columns', None)
if include_columns:
for c in self.c:
if c.name not in include_columns:
self.c.remove(c)
for key in ('quote', 'quote_schema'):
if key in kwargs:
setattr(self, key, kwargs.pop(key))
if 'info' in kwargs:
self._info = kwargs.pop('info')
self.__extra_kwargs(**kwargs)
self.__post_init(*args, **kwargs)
def _cant_override(self, *args, **kwargs):
"""Return True if any argument is not supported as an override.
Takes arguments that would be sent to Table.__init__, and returns
True if any of them would be disallowed if sent to an existing
Table singleton.
"""
return bool(args) or bool(set(kwargs).difference(
['autoload', 'autoload_with', 'schema', 'owner']))
def __extra_kwargs(self, **kwargs):
# validate remaining kwargs that they all specify DB prefixes
if len([k for k in kwargs
if not re.match(r'^(?:%s)_' % '|'.join(databases.__all__), k)]):
raise TypeError(
"Invalid argument(s) for Table: %s" % repr(kwargs.keys()))
self.kwargs.update(kwargs)
def __post_init(self, *args, **kwargs):
self._init_items(*args)
@property
def key(self):
return _get_table_key(self.name, self.schema)
def _set_primary_key(self, pk):
if getattr(self, '_primary_key', None) in self.constraints:
self.constraints.remove(self._primary_key)
self._primary_key = pk
self.constraints.add(pk)
def primary_key(self):
return self._primary_key
primary_key = property(primary_key, _set_primary_key)
def __repr__(self):
return "Table(%s)" % ', '.join(
[repr(self.name)] + [repr(self.metadata)] +
[repr(x) for x in self.columns] +
["%s=%s" % (k, repr(getattr(self, k))) for k in ['schema']])
def __str__(self):
return _get_table_key(self.description, self.schema)
def append_column(self, column):
"""Append a ``Column`` to this ``Table``."""
column._set_parent(self)
def append_constraint(self, constraint):
"""Append a ``Constraint`` to this ``Table``."""
constraint._set_parent(self)
def append_ddl_listener(self, event, listener):
"""Append a DDL event listener to this ``Table``.
The ``listener`` callable will be triggered when this ``Table`` is
created or dropped, either directly before or after the DDL is issued
to the database. The listener may modify the Table, but may not abort
the event itself.
Arguments are:
event
One of ``Table.ddl_events``; e.g. 'before-create', 'after-create',
'before-drop' or 'after-drop'.
listener
A callable, invoked with three positional arguments:
event
The event currently being handled
schema_item
The ``Table`` object being created or dropped
bind
The ``Connection`` bueing used for DDL execution.
Listeners are added to the Table's ``ddl_listeners`` attribute.
"""
if event not in self.ddl_events:
raise LookupError(event)
self.ddl_listeners[event].append(listener)
def _set_parent(self, metadata):
metadata.tables[_get_table_key(self.name, self.schema)] = self
self.metadata = metadata
def get_children(self, column_collections=True, schema_visitor=False, **kwargs):
if not schema_visitor:
return expression.TableClause.get_children(
self, column_collections=column_collections, **kwargs)
else:
if column_collections:
return [c for c in self.columns]
else:
return []
def exists(self, bind=None):
"""Return True if this table exists."""
if bind is None:
bind = _bind_or_error(self)
def do(conn):
return conn.dialect.has_table(conn, self.name, schema=self.schema)
return bind.run_callable(do)
def create(self, bind=None, checkfirst=False):
"""Issue a ``CREATE`` statement for this table.
See also ``metadata.create_all()``.
"""
self.metadata.create_all(bind=bind, checkfirst=checkfirst, tables=[self])
def drop(self, bind=None, checkfirst=False):
"""Issue a ``DROP`` statement for this table.
See also ``metadata.drop_all()``.
"""
self.metadata.drop_all(bind=bind, checkfirst=checkfirst, tables=[self])
def tometadata(self, metadata, schema=None):
"""Return a copy of this ``Table`` associated with a different ``MetaData``."""
try:
if not schema:
schema = self.schema
key = _get_table_key(self.name, schema)
return metadata.tables[key]
except KeyError:
args = []
for c in self.columns:
args.append(c.copy(schema=schema))
for c in self.constraints:
args.append(c.copy(schema=schema))
return Table(self.name, metadata, schema=schema, *args)
class Column(SchemaItem, expression.ColumnClause):
"""Represents a column in a database table."""
__visit_name__ = 'column'
def __init__(self, *args, **kwargs):
"""
Construct a new ``Column`` object.
:param name: The name of this column as represented in the database.
This argument may be the first positional argument, or specified
via keyword.
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. Names with any number of upper
case characters will be quoted and sent exactly. Note that this
behavior applies even for databases which standardize upper
case names as case insensitive such as Oracle.
The name field may be omitted at construction time and applied
later, at any time before the Column is associated with a
:class:`Table`. This is to support convenient
usage within the :mod:`~sqlalchemy.ext.declarative` extension.
:param type\_: The column's type, indicated using an instance which
subclasses :class:`~sqlalchemy.types.AbstractType`. If no arguments
are required for the type, the class of the type can be sent
as well, e.g.::
# use a type with arguments
Column('data', String(50))
# use no arguments
Column('level', Integer)
The ``type`` argument may be the second positional argument
or specified by keyword.
If this column also contains a :class:`ForeignKey`,
the type argument may be left as ``None`` in which case the
type assigned will be that of the referenced column.
:param \*args: Additional positional arguments include various
:class:`SchemaItem` derived constructs which will be applied
as options to the column. These include instances of
:class:`Constraint`, :class:`ForeignKey`, :class:`ColumnDefault`,
and :class:`Sequence`. In some cases an equivalent keyword
argument is available such as ``server_default``, ``default``
and ``unique``.
:param autoincrement: This flag may be set to ``False`` to disable
SQLAlchemy indicating at the DDL level that an integer primary
key column should have autoincrementing behavior. This
is an oft misunderstood flag and has no effect whatsoever unless
all of the following conditions are met:
* The column is of the :class:`~sqlalchemy.types.Integer` datatype.
* The column has the ``primary_key`` flag set, or is otherwise
a member of a :class:`PrimaryKeyConstraint` on this table.
* a CREATE TABLE statement is being issued via :meth:`create()`
or :meth:`create_all()`. The flag has no relevance at any
other time.
* The database supports autoincrementing behavior, such as
PostgreSQL or MySQL, and this behavior can be disabled (which does
not include SQLite).
:param default: A scalar, Python callable, or :class:`~sqlalchemy.sql.expression.ClauseElement`
representing the *default value* for this column, which will be
invoked upon insert if this column is otherwise not specified
in the VALUES clause of the insert. This is a shortcut
to using :class:`ColumnDefault` as a positional argument.
Contrast this argument to ``server_default`` which creates a
default generator on the database side.
:param key: An optional string identifier which will identify this ``Column``
object on the :class:`Table`. When a key is provided, this is the
only identifier referencing the ``Column`` within the application,
including ORM attribute mapping; the ``name`` field is used only
when rendering SQL.
:param index: When ``True``, indicates that the column is indexed.
This is a shortcut for using a :class:`Index` construct on the table.
To specify indexes with explicit names or indexes that contain multiple
columns, use the :class:`Index` construct instead.
:param info: A dictionary which defaults to ``{}``. A space to store application
specific data. This must be a dictionary.
:param nullable: If set to the default of ``True``, indicates the column
will be rendered as allowing NULL, else it's rendered as NOT NULL.
This parameter is only used when issuing CREATE TABLE statements.
:param onupdate: A scalar, Python callable, or :class:`~sqlalchemy.sql.expression.ClauseElement`
representing a default value to be applied to the column within UPDATE
statements, which wil be invoked upon update if this column is not present
in the SET clause of the update. This is a shortcut to using
:class:`ColumnDefault` as a positional argument with ``for_update=True``.
:param primary_key: If ``True``, marks this column as a primary key
column. Multiple columns can have this flag set to specify composite
primary keys. As an alternative, the primary key of a :class:`Table` can
be specified via an explicit :class:`PrimaryKeyConstraint` object.
:param server_default: A :class:`FetchedValue` instance, str, Unicode or
:func:`~sqlalchemy.sql.expression.text` construct representing
the DDL DEFAULT value for the column.
String types will be emitted as-is, surrounded by single quotes::
Column('x', Text, server_default="val")
x TEXT DEFAULT 'val'
A :func:`~sqlalchemy.sql.expression.text` expression will be
rendered as-is, without quotes::
Column('y', DateTime, server_default=text('NOW()'))0
y DATETIME DEFAULT NOW()
Strings and text() will be converted into a :class:`DefaultClause`
object upon initialization.
Use :class:`FetchedValue` to indicate that an already-existing column will generate
a default value on the database side which will be available to SQLAlchemy
for post-fetch after inserts.
This construct does not specify any DDL and the implementation is
left to the database, such as via a trigger.
:param server_onupdate: A :class:`FetchedValue` instance representing
a database-side default generation function. This indicates to
SQLAlchemy that a newly generated value will be available after updates.
This construct does not specify any DDL and the implementation is
left to the database, such as via a trigger.
:param quote: Force quoting of this column'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.
:param unique: When ``True``, indicates that this column contains a unique
constraint, or if ``index`` is ``True`` as well, indicates that the
:class:`Index` should be created with the unique flag. To specify multiple
columns in the constraint/index or to specify an explicit name,
use the :class:`UniqueConstraint` or :class:`Index` constructs explicitly.
"""
name = kwargs.pop('name', None)
type_ = kwargs.pop('type_', None)
if args:
args = list(args)
if isinstance(args[0], basestring):
if name is not None:
raise exc.ArgumentError(
"May not pass name positionally and as a keyword.")
name = args.pop(0)
if args:
coltype = args[0]
# adjust for partials
if util.callable(coltype):
coltype = args[0]()
if (isinstance(coltype, types.AbstractType) or
(isinstance(coltype, type) and
issubclass(coltype, types.AbstractType))):
if type_ is not None:
raise exc.ArgumentError(
"May not pass type_ positionally and as a keyword.")
type_ = args.pop(0)
super(Column, self).__init__(name, None, type_)
self.args = args
self.key = kwargs.pop('key', name)
self.primary_key = kwargs.pop('primary_key', False)
self.nullable = kwargs.pop('nullable', not self.primary_key)
self.default = kwargs.pop('default', None)
self.server_default = kwargs.pop('server_default', None)
self.server_onupdate = kwargs.pop('server_onupdate', None)
self.index = kwargs.pop('index', None)
self.unique = kwargs.pop('unique', None)
self.quote = kwargs.pop('quote', None)
self.onupdate = kwargs.pop('onupdate', None)
self.autoincrement = kwargs.pop('autoincrement', True)
self.constraints = set()
self.foreign_keys = util.OrderedSet()
util.set_creation_order(self)
if kwargs.get('info'):
self._info = kwargs.pop('info')
if kwargs:
raise exc.ArgumentError(
"Unknown arguments passed to Column: " + repr(kwargs.keys()))
def __str__(self):
if self.name is None:
return "(no name)"
elif self.table is not None:
if self.table.named_with_column:
return (self.table.description + "." + self.description)
else:
return self.description
else:
return self.description
@property
def bind(self):
return self.table.bind
def references(self, column):
"""Return True if this Column references the given column via foreign key."""
for fk in self.foreign_keys:
if fk.references(column.table):
return True
else:
return False
def append_foreign_key(self, fk):
fk._set_parent(self)
def __repr__(self):
kwarg = []
if self.key != self.name:
kwarg.append('key')
if self.primary_key:
kwarg.append('primary_key')
if not self.nullable:
kwarg.append('nullable')
if self.onupdate:
kwarg.append('onupdate')
if self.default:
kwarg.append('default')
if self.server_default:
kwarg.append('server_default')
return "Column(%s)" % ', '.join(
[repr(self.name)] + [repr(self.type)] +
[repr(x) for x in self.foreign_keys if x is not None] +
[repr(x) for x in self.constraints] +
[(self.table and "table=<%s>" % self.table.description or "")] +
["%s=%s" % (k, repr(getattr(self, k))) for k in kwarg])
def _set_parent(self, table):
if self.name is None:
raise exc.ArgumentError(
"Column must be constructed with a name or assign .name "
"before adding to a Table.")
if self.key is None:
self.key = self.name
self.metadata = table.metadata
if getattr(self, 'table', None) is not None:
raise exc.ArgumentError("this Column already has a table!")
if self.key in table._columns:
# note the column being replaced, if any
self._pre_existing_column = table._columns.get(self.key)
table._columns.replace(self)
if self.primary_key:
table.primary_key.replace(self)
elif self.key in table.primary_key:
raise exc.ArgumentError(
"Trying to redefine primary-key column '%s' as a "
"non-primary-key column on table '%s'" % (
self.key, table.fullname))
# if we think this should not raise an error, we'd instead do this:
#table.primary_key.remove(self)
self.table = table
if self.index:
if isinstance(self.index, basestring):
raise exc.ArgumentError(
"The 'index' keyword argument on Column is boolean only. "
"To create indexes with a specific name, create an "
"explicit Index object external to the Table.")
Index('ix_%s' % self._label, self, unique=self.unique)
elif self.unique:
if isinstance(self.unique, basestring):
raise exc.ArgumentError(
"The 'unique' keyword argument on Column is boolean only. "
"To create unique constraints or indexes with a specific "
"name, append an explicit UniqueConstraint to the Table's "
"list of elements, or create an explicit Index object "
"external to the Table.")
table.append_constraint(UniqueConstraint(self.key))
toinit = list(self.args)
if self.default is not None:
if isinstance(self.default, ColumnDefault):
toinit.append(self.default)
else:
toinit.append(ColumnDefault(self.default))
if self.server_default is not None:
if isinstance(self.server_default, FetchedValue):
toinit.append(self.server_default)
else:
toinit.append(DefaultClause(self.server_default))
if self.onupdate is not None:
toinit.append(ColumnDefault(self.onupdate, for_update=True))
if self.server_onupdate is not None:
if isinstance(self.server_onupdate, FetchedValue):
toinit.append(self.server_default)
else:
toinit.append(DefaultClause(self.server_onupdate,
for_update=True))
self._init_items(*toinit)
self.args = None
def copy(self, **kw):
"""Create a copy of this ``Column``, unitialized.
This is used in ``Table.tometadata``.
"""
return Column(self.name, self.type, self.default, key = self.key, primary_key = self.primary_key, nullable = self.nullable, quote=self.quote, index=self.index, autoincrement=self.autoincrement, *[c.copy(**kw) for c in self.constraints])
def _make_proxy(self, selectable, name=None):
"""Create a *proxy* for this column.
This is a copy of this ``Column`` referenced by a different parent
(such as an alias or select statement).
"""
fk = [ForeignKey(f.column) for f in self.foreign_keys]
c = Column(
name or self.name,
self.type,
self.default,
key = name or self.key,
primary_key = self.primary_key,
nullable = self.nullable,
quote=self.quote, *fk)
c.table = selectable
c.proxies = [self]
selectable.columns.add(c)
if self.primary_key:
selectable.primary_key.add(c)
[c._init_items(f) for f in fk]
return c
def get_children(self, schema_visitor=False, **kwargs):
if schema_visitor:
return [x for x in (self.default, self.onupdate) if x is not None] + \
list(self.foreign_keys) + list(self.constraints)
else:
return expression.ColumnClause.get_children(self, **kwargs)
class ForeignKey(SchemaItem):
"""Defines a column-level FOREIGN KEY constraint between two columns.
``ForeignKey`` is specified as an argument to a :class:`Column` object, e.g.::
t = Table("remote_table", metadata,
Column("remote_id", ForeignKey("main_table.id"))
)
For a composite (multiple column) FOREIGN KEY, use a :class:`ForeignKeyConstraint`
object specified at the level of the :class:`Table`.
Further examples of foreign key configuration are in :ref:`metadata_foreignkeys`.
"""
__visit_name__ = 'foreign_key'
def __init__(self, column, constraint=None, use_alter=False, name=None, onupdate=None, ondelete=None, deferrable=None, initially=None, link_to_name=False):
"""
Construct a column-level FOREIGN KEY.
:param column: A single target column for the key relationship. A :class:`Column`
object or a column name as a string: ``tablename.columnkey`` or
``schema.tablename.columnkey``. ``columnkey`` is the ``key`` which has been assigned
to the column (defaults to the column name itself), unless ``link_to_name`` is ``True``
in which case the rendered name of the column is used.
:param constraint: Optional. A parent :class:`ForeignKeyConstraint` object. If not
supplied, a :class:`ForeignKeyConstraint` will be automatically created
and added to the parent table.
:param name: Optional string. An in-database name for the key if `constraint` is
not provided.
:param onupdate: Optional string. If set, emit ON UPDATE