From: Mike Bayer Date: Sat, 19 Sep 2015 15:58:50 +0000 (-0400) Subject: - The use of a :class:`.postgresql.ARRAY` object that refers X-Git-Tag: rel_1_1_0b1~84^2~70^2~104 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=371f1a82c5981156a359f690923840d2627c9a6f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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". fixes #2729 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 8246234210..2f49a6bdb8 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,19 @@ .. 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 diff --git a/doc/build/changelog/migration_11.rst b/doc/build/changelog/migration_11.rst index 367f20e629..21c9765896 100644 --- a/doc/build/changelog/migration_11.rst +++ b/doc/build/changelog/migration_11.rst @@ -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 ============================================= diff --git a/lib/sqlalchemy/dialects/postgresql/array.py b/lib/sqlalchemy/dialects/postgresql/array.py index ebdfe16953..b88f139dea 100644 --- a/lib/sqlalchemy/dialects/postgresql/array.py +++ b/lib/sqlalchemy/dialects/postgresql/array.py @@ -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) diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index 7aad23255b..6ed90c76d6 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -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