diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-19 12:31:19 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-19 12:31:19 -0400 |
commit | c33d0378802abbc729de55ba205a4309e5d68f6b (patch) | |
tree | 1e8994db447e9908827c6494db8d0241a7a6de52 /lib/sqlalchemy | |
parent | 1fb4ad75a38ce84d0e7b170927025500b73b5519 (diff) | |
download | sqlalchemy-c33d0378802abbc729de55ba205a4309e5d68f6b.tar.gz |
- Liberalized the contract for :class:`.Index` a bit in that you can
specify a :func:`.text` expression as the target; the index no longer
needs to have a table-bound column present if the index is to be
manually added to the table, either via inline declaration or via
:meth:`.Table.append_constraint`. fixes #3028
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/sql/base.py | 3 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/naming.py | 11 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/schema.py | 39 | ||||
-rw-r--r-- | lib/sqlalchemy/util/__init__.py | 2 | ||||
-rw-r--r-- | lib/sqlalchemy/util/compat.py | 4 |
5 files changed, 46 insertions, 13 deletions
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index 59f30ed69..28f324ad9 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -489,6 +489,9 @@ class ColumnCollection(util.OrderedProperties): for this dictionary. """ + if not column.key: + raise exc.ArgumentError( + "Can't add unnamed column to column collection") self[column.key] = column def __delitem__(self, key): diff --git a/lib/sqlalchemy/sql/naming.py b/lib/sqlalchemy/sql/naming.py index 1c5fae193..34a72a011 100644 --- a/lib/sqlalchemy/sql/naming.py +++ b/lib/sqlalchemy/sql/naming.py @@ -158,8 +158,9 @@ def _constraint_name(const, table): metadata = table.metadata convention = _get_convention(metadata.naming_convention, type(const)) if convention is not None: - newname = conv( - convention % ConventionDict(const, table, metadata.naming_convention) - ) - if const.name is None: - const.name = newname + if const.name is None or "constraint_name" in convention: + newname = conv( + convention % ConventionDict(const, table, metadata.naming_convention) + ) + if const.name is None: + const.name = newname diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index e29fe456f..2aad60c8f 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2724,13 +2724,41 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): Index("some_index", sometable.c.name, sometable.c.address) - Functional indexes are supported as well, keeping in mind that at least - one :class:`.Column` must be present:: + Functional indexes are supported as well, typically by using the + :data:`.func` construct in conjunction with table-bound + :class:`.Column` objects:: Index("some_index", func.lower(sometable.c.name)) .. versionadded:: 0.8 support for functional and expression-based indexes. + An :class:`.Index` can also be manually associated with a :class:`.Table`, + either through inline declaration or using :meth:`.Table.append_constraint`. + When this approach is used, the names of the indexed columns can be specified + as strings:: + + Table("sometable", metadata, + Column("name", String(50)), + Column("address", String(100)), + Index("some_index", "name", "address") + ) + + To support functional or expression-based indexes in this form, the + :func:`.text` construct may be used:: + + from sqlalchemy import text + + Table("sometable", metadata, + Column("name", String(50)), + Column("address", String(100)), + Index("some_index", text("lower(name)")) + ) + + .. versionadded:: 0.9.5 the :func:`.text` construct may be used to + specify :class:`.Index` expressions, provided the :class:`.Index` + is explicitly associated with the :class:`.Table`. + + .. seealso:: :ref:`schema_indexes` - General information on :class:`.Index`. @@ -2785,8 +2813,6 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): visitors.traverse(expr, {}, {'column': cols.append}) if cols: columns.append(cols[0]) - else: - columns.append(expr) self.expressions = expressions self.name = quoted_name(name, kw.pop("quote", None)) @@ -2798,7 +2824,6 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): ColumnCollectionMixin.__init__(self, *columns) - def _set_parent(self, table): ColumnCollectionMixin._set_parent(self, table) @@ -2823,7 +2848,7 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): self.expressions = [ expr if isinstance(expr, ClauseElement) else colexpr - for expr, colexpr in zip(self.expressions, self.columns) + for expr, colexpr in util.zip_longest(self.expressions, self.columns) ] @property @@ -2865,7 +2890,7 @@ class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem): return 'Index(%s)' % ( ", ".join( [repr(self.name)] + - [repr(c) for c in self.columns] + + [repr(e) for e in self.expressions] + (self.unique and ["unique=True"] or []) )) diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py index eba64ed15..79c2d689f 100644 --- a/lib/sqlalchemy/util/__init__.py +++ b/lib/sqlalchemy/util/__init__.py @@ -10,7 +10,7 @@ from .compat import callable, cmp, reduce, \ raise_from_cause, text_type, string_types, int_types, binary_type, \ quote_plus, with_metaclass, print_, itertools_filterfalse, u, ue, b,\ unquote_plus, unquote, b64decode, b64encode, byte_buffer, itertools_filter,\ - iterbytes, StringIO, inspect_getargspec + iterbytes, StringIO, inspect_getargspec, zip_longest from ._collections import KeyedTuple, ImmutableContainer, immutabledict, \ Properties, OrderedProperties, ImmutableProperties, OrderedDict, \ diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index f1346406e..ac0478b39 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -85,6 +85,8 @@ if py3k: itertools_filterfalse = itertools.filterfalse itertools_filter = filter itertools_imap = map + from itertools import zip_longest + import base64 def b64encode(x): @@ -147,6 +149,8 @@ else: itertools_filterfalse = itertools.ifilterfalse itertools_filter = itertools.ifilter itertools_imap = itertools.imap + from itertools import izip_longest as zip_longest + import time |