]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The :class:`.CreateColumn` construct can be appled to a custom
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 24 Aug 2013 17:55:14 +0000 (13:55 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 24 Aug 2013 17:56:01 +0000 (13:56 -0400)
compilation rule which allows skipping of columns, by producing
a rule that returns ``None``.  Also in 0.8.3.

doc/build/changelog/changelog_08.rst
doc/build/changelog/changelog_09.rst
lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/ddl.py
test/ext/test_compiler.py

index 39a8627bad745d32338214b5795fa39a493cca7b..edfa4b63f7e8591cde88de1a0300355dc87d3972 100644 (file)
@@ -6,6 +6,13 @@
 .. changelog::
     :version: 0.8.3
 
+    .. change::
+        :tags: feature
+
+        The :class:`.CreateColumn` construct can be appled to a custom
+        compilation rule which allows skipping of columns, by producing
+        a rule that returns ``None``.
+
     .. change::
         :tags: bug, orm
         :tickets: 2807
index d928120f9b8bd4af5ce4514f601dafdffde558a5..7e6f4e1672fe521fe3fe3e084693315400bbb09b 100644 (file)
@@ -6,6 +6,13 @@
 .. changelog::
     :version: 0.9.0
 
+    .. change::
+        :tags: feature
+
+        The :class:`.CreateColumn` construct can be appled to a custom
+        compilation rule which allows skipping of columns, by producing
+        a rule that returns ``None``.  Also in 0.8.3.
+
     .. change::
         :tags: bug, orm
         :tickets: 2807
index dc3d55039d1cafe704108cc42e5b4aed23b96b3c..c56ca1ae438ffd36d6f135d1d9c883ac2bc9cabd 100644 (file)
@@ -2178,11 +2178,13 @@ class DDLCompiler(Compiled):
         for create_column in create.columns:
             column = create_column.element
             try:
-                text += separator
-                separator = ", \n"
-                text += "\t" + self.process(create_column,
+                processed = self.process(create_column,
                                     first_pk=column.primary_key
                                     and not first_pk)
+                if processed is not None:
+                    text += separator
+                    separator = ", \n"
+                    text += "\t" + processed
                 if column.primary_key:
                     first_pk = True
             except exc.CompileError as ce:
index 8159ca4721016b5a51841e32427ab7b75671b015..a0163ad7a4bcf585eb421eac108f86be50e11c61 100644 (file)
@@ -548,6 +548,35 @@ class CreateColumn(_DDLCompiles):
             PRIMARY KEY (x)
         )
 
+    The :class:`.CreateColumn` construct can also be used to skip certain
+    columns when producing a ``CREATE TABLE``.  This is accomplished by
+    creating a compilation rule that conditionally returns ``None``.
+    For example, to produce a
+    :class:`.Table` which includes the Postgresql system column ``xmin``,
+    but omits this column from the ``CREATE TABLE``::
+
+        from sqlalchemy.schema import CreateColumn
+
+        @compiles(CreateColumn)
+        def skip_xmin(element, compiler, **kw):
+            if element.element.name == 'xmin':
+                return None
+            else:
+                return compiler.visit_create_column(element, **kw)
+
+
+        my_table = Table('mytable', metadata,
+                    Column('id', Integer, primary_key=True),
+                    Column('xmin', Integer)
+                )
+
+    Above, a :class:`.CreateTable` construct will generate a ``CREATE TABLE``
+    which only includes the ``id`` column in the string; the ``xmin`` column
+    will be omitted.
+
+    .. versionadded:: 0.8.3 The :class:`.CreateColumn` construct supports
+       skipping of columns by returning ``None`` from a custom compilation rule.
+
     .. versionadded:: 0.8 The :class:`.CreateColumn` construct was added
        to support custom column creation styles.
 
index c1f8b6258989cb0f90ea6f523928c3d49b12c295..5ed50442f7b8da31e7eaafe17400f2339a90b893 100644 (file)
@@ -4,7 +4,7 @@ from sqlalchemy.sql.expression import ClauseElement, ColumnClause,\
                                     FunctionElement, Select, \
                                     BindParameter
 
-from sqlalchemy.schema import DDLElement
+from sqlalchemy.schema import DDLElement, CreateColumn, CreateTable
 from sqlalchemy.ext.compiler import compiles, deregister
 from sqlalchemy import exc
 from sqlalchemy.sql import table, column, visitors
@@ -34,6 +34,22 @@ class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL):
             "SELECT >>x<<, >>y<< WHERE >>MYTHINGY!<< = :MYTHINGY!_1"
         )
 
+    def test_create_column_skip(self):
+        @compiles(CreateColumn)
+        def skip_xmin(element, compiler, **kw):
+            if element.element.name == 'xmin':
+                return None
+            else:
+                return compiler.visit_create_column(element, **kw)
+
+        t = Table('t', MetaData(), Column('a', Integer),
+                            Column('xmin', Integer),
+                            Column('c', Integer))
+
+        self.assert_compile(
+            CreateTable(t),
+            "CREATE TABLE t (a INTEGER, c INTEGER)"
+        )
     def test_types(self):
         class MyType(TypeEngine):
             pass