~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/schema.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2013-10-28 22:29:40 UTC
  • mfrom: (1.4.24)
  • Revision ID: package-import@ubuntu.com-20131028222940-wvyqffl4g617caun
Tags: 0.8.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
123
123
 
124
 
    See also:
 
124
    .. seealso::
125
125
 
126
 
    :ref:`metadata_describing` - Introduction to database metadata
 
126
        :ref:`metadata_describing` - Introduction to database metadata
127
127
 
128
128
    Constructor arguments are as follows:
129
129
 
603
603
        :class:`.Table`, using the given :class:`.Connectable`
604
604
        for connectivity.
605
605
 
606
 
        See also :meth:`.MetaData.create_all`.
 
606
        .. seealso::
 
607
 
 
608
             :meth:`.MetaData.create_all`.
607
609
 
608
610
        """
609
611
 
618
620
        :class:`.Table`, using the given :class:`.Connectable`
619
621
        for connectivity.
620
622
 
621
 
        See also :meth:`.MetaData.drop_all`.
 
623
        .. seealso::
 
624
 
 
625
            :meth:`.MetaData.drop_all`.
622
626
 
623
627
        """
624
628
        if bind is None:
892
896
             an explicit name, use the :class:`.UniqueConstraint` or
893
897
             :class:`.Index` constructs explicitly.
894
898
 
 
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.
 
903
 
 
904
             For more elaborate scenarios where columns should be conditionally
 
905
             rendered differently on different backends, consider custom
 
906
             compilation rules for :class:`.CreateColumn`.
 
907
 
 
908
             ..versionadded:: 0.8.3 Added the ``system=True`` parameter to
 
909
               :class:`.Column`.
 
910
 
895
911
        """
896
912
 
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)
 
942
 
 
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)
 
948
 
 
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(
1114
1136
                primary_key=self.primary_key,
1115
1137
                nullable=self.nullable,
1116
1138
                unique=self.unique,
 
1139
                system=self.system,
1117
1140
                quote=self.quote,
1118
1141
                index=self.index,
1119
1142
                autoincrement=self.autoincrement,
1282
1305
            DDL for this constraint. Typical values include SIMPLE, PARTIAL
1283
1306
            and FULL.
1284
1307
 
 
1308
        :param schema: Deprecated; this flag does nothing and will be removed
 
1309
            in 0.9.
1285
1310
        """
1286
1311
 
1287
1312
        self._colspec = column
1302
1327
        self.link_to_name = link_to_name
1303
1328
        self.match = match
1304
1329
 
 
1330
        if schema:
 
1331
            util.warn_deprecated(
 
1332
                "'schema' argument on ForeignKey has no effect - "
 
1333
                "please specify the target as "
 
1334
                "<schemaname>.<tablename>.<colname>.")
 
1335
 
1305
1336
    def __repr__(self):
1306
1337
        return "ForeignKey(%r)" % self._get_colspec()
1307
1338
 
1702
1733
    be emitted as well.   For platforms that don't support sequences,
1703
1734
    the :class:`.Sequence` construct is ignored.
1704
1735
 
1705
 
    See also: :class:`.CreateSequence` :class:`.DropSequence`
 
1736
    .. seealso::
 
1737
 
 
1738
        :class:`.CreateSequence`
 
1739
 
 
1740
        :class:`.DropSequence`
1706
1741
 
1707
1742
    """
1708
1743
 
2350
2385
class Index(ColumnCollectionMixin, SchemaItem):
2351
2386
    """A table-level INDEX.
2352
2387
 
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.
 
2389
 
 
2390
    E.g.::
 
2391
 
 
2392
        sometable = Table("sometable", metadata,
 
2393
                        Column("name", String(50)),
 
2394
                        Column("address", String(100))
 
2395
                    )
 
2396
 
 
2397
        Index("some_index", sometable.c.name)
 
2398
 
 
2399
    For a no-frills, single column index, adding
 
2400
    :class:`.Column` also supports ``index=True``::
 
2401
 
 
2402
        sometable = Table("sometable", metadata,
 
2403
                        Column("name", String(50), index=True)
 
2404
                    )
 
2405
 
 
2406
    For a composite index, multiple columns can be specified::
 
2407
 
 
2408
        Index("some_index", sometable.c.name, sometable.c.address)
 
2409
 
 
2410
    Functional indexes are supported as well, keeping in mind that at least
 
2411
    one :class:`.Column` must be present::
 
2412
 
 
2413
        Index("some_index", func.lower(sometable.c.name))
 
2414
 
 
2415
    .. versionadded:: 0.8 support for functional and expression-based indexes.
2356
2416
 
2357
2417
    .. seealso::
2358
2418
 
2378
2438
          The name of the index
2379
2439
 
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
2384
 
          :class:`.Column`.
2385
 
 
2386
 
          .. versionadded:: 0.8 :class:`.Index` supports SQL expressions as
2387
 
             well as plain columns.
 
2441
          Column or SQL expressions.
2388
2442
 
2389
2443
        :param unique:
2390
2444
            Defaults to False: create a unique index.
2455
2509
        :class:`.Index`, using the given :class:`.Connectable`
2456
2510
        for connectivity.
2457
2511
 
2458
 
        See also :meth:`.MetaData.create_all`.
 
2512
        .. seealso::
 
2513
 
 
2514
            :meth:`.MetaData.create_all`.
2459
2515
 
2460
2516
        """
2461
2517
        if bind is None:
2468
2524
        :class:`.Index`, using the given :class:`.Connectable`
2469
2525
        for connectivity.
2470
2526
 
2471
 
        See also :meth:`.MetaData.drop_all`.
 
2527
        .. seealso::
 
2528
 
 
2529
            :meth:`.MetaData.drop_all`.
2472
2530
 
2473
2531
        """
2474
2532
        if bind is None:
2510
2568
    MetaData is a thread-safe object after tables have been explicitly defined
2511
2569
    or loaded via reflection.
2512
2570
 
2513
 
    See also:
2514
 
 
2515
 
    :ref:`metadata_describing` - Introduction to database metadata
2516
 
 
2517
 
    .. index::
2518
 
      single: thread safety; MetaData
 
2571
    .. seealso::
 
2572
 
 
2573
        :ref:`metadata_describing` - Introduction to database metadata
2519
2574
 
2520
2575
    """
2521
2576
 
2917
2972
            AddConstraint(constraint).execute_if(dialect='postgresql')
2918
2973
        )
2919
2974
 
2920
 
    See also:
 
2975
    .. seealso::
2921
2976
 
2922
2977
        :class:`.DDL`
2923
2978
 
3069
3124
        :param state: any value which will be passed to the callable_
3070
3125
          as the ``state`` keyword argument.
3071
3126
 
3072
 
        See also:
 
3127
        .. seealso::
3073
3128
 
3074
3129
            :class:`.DDLEvents`
3075
3130
 
3233
3288
          default when ``execute()`` is invoked without a bind argument.
3234
3289
 
3235
3290
 
3236
 
        See also:
 
3291
        .. seealso::
3237
3292
 
3238
3293
            :class:`.DDLEvents`
 
3294
 
3239
3295
            :mod:`sqlalchemy.event`
3240
3296
 
3241
3297
        """
3437
3493
            PRIMARY KEY (x)
3438
3494
        )
3439
3495
 
 
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.
 
3502
 
 
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::
 
3507
 
 
3508
        from sqlalchemy.schema import CreateColumn
 
3509
 
 
3510
        @compiles(CreateColumn, "postgresql")
 
3511
        def skip_xmin(element, compiler, **kw):
 
3512
            if element.element.name == 'xmin':
 
3513
                return None
 
3514
            else:
 
3515
                return compiler.visit_create_column(element, **kw)
 
3516
 
 
3517
 
 
3518
        my_table = Table('mytable', metadata,
 
3519
                    Column('id', Integer, primary_key=True),
 
3520
                    Column('xmin', Integer)
 
3521
                )
 
3522
 
 
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.
 
3526
 
 
3527
    .. versionadded:: 0.8.3 The :class:`.CreateColumn` construct supports
 
3528
       skipping of columns by returning ``None`` from a custom compilation rule.
 
3529
 
3440
3530
    .. versionadded:: 0.8 The :class:`.CreateColumn` construct was added
3441
3531
       to support custom column creation styles.
3442
3532