]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The use of a :class:`.postgresql.ARRAY` object that refers
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 19 Sep 2015 15:58:50 +0000 (11:58 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 19 Sep 2015 15:58:50 +0000 (11:58 -0400)
to a :class:`.types.Enum` or :class:`.postgresql.ENUM` subtype
will now emit the expected "CREATE TYPE" and "DROP TYPE" DDL when
the type is used within a "CREATE TABLE" or "DROP TABLE".
fixes #2729

doc/build/changelog/changelog_11.rst
doc/build/changelog/migration_11.rst
lib/sqlalchemy/dialects/postgresql/array.py
test/dialect/postgresql/test_types.py

index 824623421058f1e0653740e70d79ef1de602f7e0..2f49a6bdb8cd5b153ddf84df0f5563d5779a2cc5 100644 (file)
 .. changelog::
     :version: 1.1.0b1
 
+    .. change::
+        :tags: bug, postgresql
+        :tickets: 2729
+
+        The use of a :class:`.postgresql.ARRAY` object that refers
+        to a :class:`.types.Enum` or :class:`.postgresql.ENUM` subtype
+        will now emit the expected "CREATE TYPE" and "DROP TYPE" DDL when
+        the type is used within a "CREATE TABLE" or "DROP TABLE".
+
+        .. seealso::
+
+            :ref:`change_2729`
+
     .. change::
         :tags: bug, sql
         :tickets: 3531
index 367f20e629608fea8899c1713df9d5a9d124f972..21c97658964d2892dfd2ddab67afde4e696ee33d 100644 (file)
@@ -723,6 +723,44 @@ to::
 
 :ticket:`3514`
 
+.. _change_2729:
+
+ARRAY with ENUM will now emit CREATE TYPE for the ENUM
+------------------------------------------------------
+
+A table definition like the following will now emit CREATE TYPE
+as expected::
+
+    enum = Enum(
+        'manager', 'place_admin', 'carwash_admin',
+        'parking_admin', 'service_admin', 'tire_admin',
+        'mechanic', 'carwasher', 'tire_mechanic', name="work_place_roles")
+
+    class WorkPlacement(Base):
+        __tablename__ = 'work_placement'
+        id = Column(Integer, primary_key=True)
+        roles = Column(ARRAY(enum))
+
+
+    e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
+    Base.metadata.create_all(e)
+
+emits::
+
+    CREATE TYPE work_place_roles AS ENUM (
+        'manager', 'place_admin', 'carwash_admin', 'parking_admin',
+        'service_admin', 'tire_admin', 'mechanic', 'carwasher',
+        'tire_mechanic')
+
+    CREATE TABLE work_placement (
+        id SERIAL NOT NULL,
+        roles work_place_roles[],
+        PRIMARY KEY (id)
+    )
+
+
+:ticket:`2729`
+
 Dialect Improvements and Changes - MySQL
 =============================================
 
index ebdfe16953ce5ba4687bde26a01e545c8dc05570..b88f139deaee2f779ab6ebf2957aeaae73cd0e01 100644 (file)
@@ -7,6 +7,7 @@
 
 from .base import ischema_names
 from ...sql import expression, operators
+from ...sql.base import SchemaEventTarget
 from ... import types as sqltypes
 
 try:
@@ -105,7 +106,7 @@ CONTAINED_BY = operators.custom_op("<@", precedence=5)
 OVERLAP = operators.custom_op("&&", precedence=5)
 
 
-class ARRAY(sqltypes.Array):
+class ARRAY(SchemaEventTarget, sqltypes.Array):
 
     """Postgresql ARRAY type.
 
@@ -239,6 +240,18 @@ class ARRAY(sqltypes.Array):
     def compare_values(self, x, y):
         return x == y
 
+    def _set_parent(self, column):
+        """Support SchemaEentTarget"""
+
+        if isinstance(self.item_type, SchemaEventTarget):
+            self.item_type._set_parent(column)
+
+    def _set_parent_with_dispatch(self, parent):
+        """Support SchemaEentTarget"""
+
+        if isinstance(self.item_type, SchemaEventTarget):
+            self.item_type._set_parent_with_dispatch(parent)
+
     def _proc_array(self, arr, itemproc, dim, collection):
         if dim is None:
             arr = list(arr)
index 7aad23255b99787ee34aeb777237a8594c49a016..6ed90c76d62ccebc2d04c0201b759c3871fb7fb6 100644 (file)
@@ -1312,6 +1312,32 @@ class ArrayRoundTripTest(fixtures.TablesTest, AssertsExecutionResults):
             set([('1', '2', '3'), ('4', '5', '6'), (('4', '5'), ('6', '7'))])
         )
 
+    def test_array_plus_native_enum_create(self):
+        m = MetaData()
+        t = Table(
+            't', m,
+            Column(
+                'data_1',
+                postgresql.ARRAY(
+                    postgresql.ENUM('a', 'b', 'c', name='my_enum_1')
+                )
+            ),
+            Column(
+                'data_2',
+                postgresql.ARRAY(
+                    types.Enum('a', 'b', 'c', name='my_enum_2')
+                )
+            )
+        )
+
+        t.create(testing.db)
+        eq_(
+            set(e['name'] for e in inspect(testing.db).get_enums()),
+            set(['my_enum_1', 'my_enum_2'])
+        )
+        t.drop(testing.db)
+        eq_(inspect(testing.db).get_enums(), [])
+
 
 class HashableFlagORMTest(fixtures.TestBase):
     """test the various 'collection' types that they flip the 'hashable' flag