Skip to content

Commit e9b2e33

Browse files
committed
- 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.
1 parent 460b237 commit e9b2e33

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

doc/build/changelog/changelog_08.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
.. changelog::
77
:version: 0.8.3
88

9+
.. change::
10+
:tags: feature
11+
12+
The :class:`.CreateColumn` construct can be appled to a custom
13+
compilation rule which allows skipping of columns, by producing
14+
a rule that returns ``None``.
15+
916
.. change::
1017
:tags: bug, orm
1118
:tickets: 2807

doc/build/changelog/changelog_09.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
.. changelog::
77
:version: 0.9.0
88

9+
.. change::
10+
:tags: feature
11+
12+
The :class:`.CreateColumn` construct can be appled to a custom
13+
compilation rule which allows skipping of columns, by producing
14+
a rule that returns ``None``. Also in 0.8.3.
15+
916
.. change::
1017
:tags: bug, orm
1118
:tickets: 2807

lib/sqlalchemy/sql/compiler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,11 +2178,13 @@ def visit_create_table(self, create):
21782178
for create_column in create.columns:
21792179
column = create_column.element
21802180
try:
2181-
text += separator
2182-
separator = ", \n"
2183-
text += "\t" + self.process(create_column,
2181+
processed = self.process(create_column,
21842182
first_pk=column.primary_key
21852183
and not first_pk)
2184+
if processed is not None:
2185+
text += separator
2186+
separator = ", \n"
2187+
text += "\t" + processed
21862188
if column.primary_key:
21872189
first_pk = True
21882190
except exc.CompileError as ce:

lib/sqlalchemy/sql/ddl.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,35 @@ def compile(element, compiler, **kw):
548548
PRIMARY KEY (x)
549549
)
550550
551+
The :class:`.CreateColumn` construct can also be used to skip certain
552+
columns when producing a ``CREATE TABLE``. This is accomplished by
553+
creating a compilation rule that conditionally returns ``None``.
554+
For example, to produce a
555+
:class:`.Table` which includes the Postgresql system column ``xmin``,
556+
but omits this column from the ``CREATE TABLE``::
557+
558+
from sqlalchemy.schema import CreateColumn
559+
560+
@compiles(CreateColumn)
561+
def skip_xmin(element, compiler, **kw):
562+
if element.element.name == 'xmin':
563+
return None
564+
else:
565+
return compiler.visit_create_column(element, **kw)
566+
567+
568+
my_table = Table('mytable', metadata,
569+
Column('id', Integer, primary_key=True),
570+
Column('xmin', Integer)
571+
)
572+
573+
Above, a :class:`.CreateTable` construct will generate a ``CREATE TABLE``
574+
which only includes the ``id`` column in the string; the ``xmin`` column
575+
will be omitted.
576+
577+
.. versionadded:: 0.8.3 The :class:`.CreateColumn` construct supports
578+
skipping of columns by returning ``None`` from a custom compilation rule.
579+
551580
.. versionadded:: 0.8 The :class:`.CreateColumn` construct was added
552581
to support custom column creation styles.
553582

test/ext/test_compiler.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
FunctionElement, Select, \
55
BindParameter
66

7-
from sqlalchemy.schema import DDLElement
7+
from sqlalchemy.schema import DDLElement, CreateColumn, CreateTable
88
from sqlalchemy.ext.compiler import compiles, deregister
99
from sqlalchemy import exc
1010
from sqlalchemy.sql import table, column, visitors
@@ -34,6 +34,22 @@ def visit_thingy(thingy, compiler, **kw):
3434
"SELECT >>x<<, >>y<< WHERE >>MYTHINGY!<< = :MYTHINGY!_1"
3535
)
3636

37+
def test_create_column_skip(self):
38+
@compiles(CreateColumn)
39+
def skip_xmin(element, compiler, **kw):
40+
if element.element.name == 'xmin':
41+
return None
42+
else:
43+
return compiler.visit_create_column(element, **kw)
44+
45+
t = Table('t', MetaData(), Column('a', Integer),
46+
Column('xmin', Integer),
47+
Column('c', Integer))
48+
49+
self.assert_compile(
50+
CreateTable(t),
51+
"CREATE TABLE t (a INTEGER, c INTEGER)"
52+
)
3753
def test_types(self):
3854
class MyType(TypeEngine):
3955
pass

0 commit comments

Comments
 (0)