]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added "system=True" to Column, so that we generally don't have to bother
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 25 Aug 2013 16:28:47 +0000 (12:28 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 25 Aug 2013 16:29:22 +0000 (12:29 -0400)
with CreateColumn rules

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

index edfa4b63f7e8591cde88de1a0300355dc87d3972..c911c61f04df94778fba70b64f1347c70dac7e28 100644 (file)
@@ -9,7 +9,12 @@
     .. change::
         :tags: feature
 
-        The :class:`.CreateColumn` construct can be appled to a custom
+        Added a new flag ``system=True`` to :class:`.Column`, which marks
+        the column as a "system" column which is automatically made present
+        by the database (such as Postgresql ``oid`` or ``xmin``).  The
+        column will be omitted from the ``CREATE TABLE`` statement but will
+        otherwise be available for querying.   In addition, the
+        :class:`.CreateColumn` construct can be appled to a custom
         compilation rule which allows skipping of columns, by producing
         a rule that returns ``None``.
 
index 7e6f4e1672fe521fe3fe3e084693315400bbb09b..5d248acec2b8558ee7058ac004dc8a97309e615c 100644 (file)
@@ -9,7 +9,12 @@
     .. change::
         :tags: feature
 
-        The :class:`.CreateColumn` construct can be appled to a custom
+        Added a new flag ``system=True`` to :class:`.Column`, which marks
+        the column as a "system" column which is automatically made present
+        by the database (such as Postgresql ``oid`` or ``xmin``).  The
+        column will be omitted from the ``CREATE TABLE`` statement but will
+        otherwise be available for querying.   In addition, 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.
 
index c56ca1ae438ffd36d6f135d1d9c883ac2bc9cabd..6370b122737014ae024191ccb9f9325ce114c075 100644 (file)
@@ -2205,6 +2205,9 @@ class DDLCompiler(Compiled):
     def visit_create_column(self, create, first_pk=False):
         column = create.element
 
+        if column.system:
+            return None
+
         text = self.get_column_specification(
                         column,
                         first_pk=first_pk
index a0163ad7a4bcf585eb421eac108f86be50e11c61..a17c8ee53f08f4c291a0c1f53d3586d3cde914db 100644 (file)
@@ -551,13 +551,18 @@ class CreateColumn(_DDLCompiles):
     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``::
+    This is essentially how to produce the same effect as using the
+    ``system=True`` argument on :class:`.Column`, which marks a column
+    as an implicitly-present "system" column.
+
+    For example, suppose we wish to produce a :class:`.Table` which skips
+    rendering of the Postgresql ``xmin`` column against the Postgresql backend,
+    but on other backends does render it, in anticipation of a triggered rule.
+    A conditional compilation rule could skip this name only on Postgresql::
 
         from sqlalchemy.schema import CreateColumn
 
-        @compiles(CreateColumn)
+        @compiles(CreateColumn, "postgresql")
         def skip_xmin(element, compiler, **kw):
             if element.element.name == 'xmin':
                 return None
@@ -572,7 +577,7 @@ class CreateColumn(_DDLCompiles):
 
     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.
+    will be omitted, but only against the Postgresql backend.
 
     .. versionadded:: 0.8.3 The :class:`.CreateColumn` construct supports
        skipping of columns by returning ``None`` from a custom compilation rule.
index ce4182c54b432e453d47010e5e9545d6b0684800..5930df1240fb00113243ddd88d86d4e263794b0f 100644 (file)
@@ -893,6 +893,18 @@ class Column(SchemaItem, ColumnClause):
              an explicit name, use the :class:`.UniqueConstraint` or
              :class:`.Index` constructs explicitly.
 
+        :param system: When ``True``, indicates this is a "system" column,
+             that is a column which is automatically made available by the
+             database, and should not be included in the columns list for a
+             ``CREATE TABLE`` statement.
+
+             For more elaborate scenarios where columns should be conditionally
+             rendered differently on different backends, consider custom
+             compilation rules for :class:`.CreateColumn`.
+
+             ..versionadded:: 0.8.3 Added the ``system=True`` parameter to
+               :class:`.Column`.
+
         """
 
         name = kwargs.pop('name', None)
@@ -922,6 +934,7 @@ class Column(SchemaItem, ColumnClause):
         self.server_onupdate = kwargs.pop('server_onupdate', None)
         self.index = kwargs.pop('index', None)
         self.unique = kwargs.pop('unique', None)
+        self.system = kwargs.pop('system', False)
         self.quote = kwargs.pop('quote', None)
         self.doc = kwargs.pop('doc', None)
         self.onupdate = kwargs.pop('onupdate', None)
index bdfcccb22ee4966e4d79b7452bf92c451c42582c..d52d8042968141406113961d0694f3f0d95475dd 100644 (file)
@@ -2800,6 +2800,15 @@ class DDLTest(fixtures.TestBase, AssertsCompiledSQL):
             schema.CreateTable(t1).compile
         )
 
+    def test_system_flag(self):
+        m = MetaData()
+        t = Table('t', m, Column('x', Integer),
+                            Column('y', Integer, system=True),
+                            Column('z', Integer))
+        self.assert_compile(
+            schema.CreateTable(t),
+            "CREATE TABLE t (x INTEGER, z INTEGER)"
+        )
 
 class InlineDefaultTest(fixtures.TestBase, AssertsCompiledSQL):
     __dialect__ = 'default'