summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/schema.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-01-16 21:04:32 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2013-01-16 21:04:32 -0500
commit7fedf9958687222a9e3c2fc4d50983711dbb3d52 (patch)
tree921320393422f906e75451376b41b3406abacef4 /lib/sqlalchemy/schema.py
parent87002643407f886f13a3b53283ea0b6dafa695cc (diff)
downloadsqlalchemy-7fedf9958687222a9e3c2fc4d50983711dbb3d52.tar.gz
:class:`.Index` now supports arbitrary SQL expressions and/or
functions, in addition to straight columns. Common modifiers include using ``somecolumn.desc()`` for a descending index and ``func.lower(somecolumn)`` for a case-insensitive index, depending on the capabilities of the target backend. [ticket:695]
Diffstat (limited to 'lib/sqlalchemy/schema.py')
-rw-r--r--lib/sqlalchemy/schema.py42
1 files changed, 36 insertions, 6 deletions
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py
index b5e97802d..5cb592857 100644
--- a/lib/sqlalchemy/schema.py
+++ b/lib/sqlalchemy/schema.py
@@ -2344,22 +2344,30 @@ class Index(ColumnCollectionMixin, SchemaItem):
:ref:`schema_indexes` - General information on :class:`.Index`.
- :ref:`postgresql_indexes` - PostgreSQL-specific options available for the :class:`.Index` construct.
+ :ref:`postgresql_indexes` - PostgreSQL-specific options available for the
+ :class:`.Index` construct.
+
+ :ref:`mysql_indexes` - MySQL-specific options available for the
+ :class:`.Index` construct.
- :ref:`mysql_indexes` - MySQL-specific options available for the :class:`.Index` construct.
"""
__visit_name__ = 'index'
- def __init__(self, name, *columns, **kw):
+ def __init__(self, name, *expressions, **kw):
"""Construct an index object.
:param name:
The name of the index
- :param \*columns:
- Columns to include in the index. All columns must belong to the same
- table.
+ :param \*expressions:
+ Column expressions to include in the index. The expressions
+ are normally instances of :class:`.Column`, but may also
+ be arbitrary SQL expressions which ultmately refer to a
+ :class:`.Column`.
+
+ .. versionadded:: 0.8 :class:`.Index` supports SQL expressions as
+ well as plain columns.
:param unique:
Defaults to False: create a unique index.
@@ -2369,9 +2377,25 @@ class Index(ColumnCollectionMixin, SchemaItem):
"""
self.table = None
+
+ columns = []
+ for expr in expressions:
+ if not isinstance(expr, expression.ClauseElement):
+ columns.append(expr)
+ else:
+ cols = []
+ visitors.traverse(expr, {}, {'column': cols.append})
+ if cols:
+ columns.append(cols[0])
+ else:
+ columns.append(expr)
+
+ self.expressions = expressions
+
# will call _set_parent() if table-bound column
# objects are present
ColumnCollectionMixin.__init__(self, *columns)
+
self.name = name
self.unique = kw.pop('unique', False)
self.kwargs = kw
@@ -2397,6 +2421,12 @@ class Index(ColumnCollectionMixin, SchemaItem):
)
table.indexes.add(self)
+ self.expressions = [
+ expr if isinstance(expr, expression.ClauseElement)
+ else colexpr
+ for expr, colexpr in zip(self.expressions, self.columns)
+ ]
+
@property
def bind(self):
"""Return the connectable associated with this Index."""