121
121
a second time will return the *same* :class:`.Table` object - in this way
122
122
the :class:`.Table` constructor acts as a registry function.
126
:ref:`metadata_describing` - Introduction to database metadata
126
:ref:`metadata_describing` - Introduction to database metadata
128
128
Constructor arguments are as follows:
892
896
an explicit name, use the :class:`.UniqueConstraint` or
893
897
:class:`.Index` constructs explicitly.
899
:param system: When ``True``, indicates this is a "system" column,
900
that is a column which is automatically made available by the
901
database, and should not be included in the columns list for a
902
``CREATE TABLE`` statement.
904
For more elaborate scenarios where columns should be conditionally
905
rendered differently on different backends, consider custom
906
compilation rules for :class:`.CreateColumn`.
908
..versionadded:: 0.8.3 Added the ``system=True`` parameter to
897
913
name = kwargs.pop('name', None)
923
939
self.default = kwargs.pop('default', None)
924
940
self.server_default = kwargs.pop('server_default', None)
925
941
self.server_onupdate = kwargs.pop('server_onupdate', None)
943
# these default to None because .index and .unique is *not*
944
# an informational flag about Column - there can still be an
945
# Index or UniqueConstraint referring to this Column.
926
946
self.index = kwargs.pop('index', None)
927
947
self.unique = kwargs.pop('unique', None)
949
self.system = kwargs.pop('system', False)
928
950
self.quote = kwargs.pop('quote', None)
929
951
self.doc = kwargs.pop('doc', None)
930
952
self.onupdate = kwargs.pop('onupdate', None)
1075
1097
"To create indexes with a specific name, create an "
1076
1098
"explicit Index object external to the Table.")
1077
1099
Index(expression._truncated_label('ix_%s' % self._label),
1078
self, unique=self.unique)
1100
self, unique=bool(self.unique))
1079
1101
elif self.unique:
1080
1102
if isinstance(self.unique, basestring):
1081
1103
raise exc.ArgumentError(
1302
1327
self.link_to_name = link_to_name
1303
1328
self.match = match
1331
util.warn_deprecated(
1332
"'schema' argument on ForeignKey has no effect - "
1333
"please specify the target as "
1334
"<schemaname>.<tablename>.<colname>.")
1305
1336
def __repr__(self):
1306
1337
return "ForeignKey(%r)" % self._get_colspec()
2350
2385
class Index(ColumnCollectionMixin, SchemaItem):
2351
2386
"""A table-level INDEX.
2353
Defines a composite (one or more column) INDEX. For a no-frills, single
2354
column index, adding ``index=True`` to the ``Column`` definition is
2355
a shorthand equivalent for an unnamed, single column :class:`.Index`.
2388
Defines a composite (one or more column) INDEX.
2392
sometable = Table("sometable", metadata,
2393
Column("name", String(50)),
2394
Column("address", String(100))
2397
Index("some_index", sometable.c.name)
2399
For a no-frills, single column index, adding
2400
:class:`.Column` also supports ``index=True``::
2402
sometable = Table("sometable", metadata,
2403
Column("name", String(50), index=True)
2406
For a composite index, multiple columns can be specified::
2408
Index("some_index", sometable.c.name, sometable.c.address)
2410
Functional indexes are supported as well, keeping in mind that at least
2411
one :class:`.Column` must be present::
2413
Index("some_index", func.lower(sometable.c.name))
2415
.. versionadded:: 0.8 support for functional and expression-based indexes.
2378
2438
The name of the index
2380
2440
:param \*expressions:
2381
Column expressions to include in the index. The expressions
2382
are normally instances of :class:`.Column`, but may also
2383
be arbitrary SQL expressions which ultmately refer to a
2386
.. versionadded:: 0.8 :class:`.Index` supports SQL expressions as
2387
well as plain columns.
2441
Column or SQL expressions.
2390
2444
Defaults to False: create a unique index.
2510
2568
MetaData is a thread-safe object after tables have been explicitly defined
2511
2569
or loaded via reflection.
2515
:ref:`metadata_describing` - Introduction to database metadata
2518
single: thread safety; MetaData
2573
:ref:`metadata_describing` - Introduction to database metadata
3437
3493
PRIMARY KEY (x)
3496
The :class:`.CreateColumn` construct can also be used to skip certain
3497
columns when producing a ``CREATE TABLE``. This is accomplished by
3498
creating a compilation rule that conditionally returns ``None``.
3499
This is essentially how to produce the same effect as using the
3500
``system=True`` argument on :class:`.Column`, which marks a column
3501
as an implicitly-present "system" column.
3503
For example, suppose we wish to produce a :class:`.Table` which skips
3504
rendering of the Postgresql ``xmin`` column against the Postgresql backend,
3505
but on other backends does render it, in anticipation of a triggered rule.
3506
A conditional compilation rule could skip this name only on Postgresql::
3508
from sqlalchemy.schema import CreateColumn
3510
@compiles(CreateColumn, "postgresql")
3511
def skip_xmin(element, compiler, **kw):
3512
if element.element.name == 'xmin':
3515
return compiler.visit_create_column(element, **kw)
3518
my_table = Table('mytable', metadata,
3519
Column('id', Integer, primary_key=True),
3520
Column('xmin', Integer)
3523
Above, a :class:`.CreateTable` construct will generate a ``CREATE TABLE``
3524
which only includes the ``id`` column in the string; the ``xmin`` column
3525
will be omitted, but only against the Postgresql backend.
3527
.. versionadded:: 0.8.3 The :class:`.CreateColumn` construct supports
3528
skipping of columns by returning ``None`` from a custom compilation rule.
3440
3530
.. versionadded:: 0.8 The :class:`.CreateColumn` construct was added
3441
3531
to support custom column creation styles.