diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-09-19 11:58:50 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-09-19 11:58:50 -0400 |
commit | 371f1a82c5981156a359f690923840d2627c9a6f (patch) | |
tree | 0557e5e42708d14959d5888d04460e03301117aa | |
parent | 53defccab70831806c5a8e192fa0ebd10df62f03 (diff) | |
download | sqlalchemy-371f1a82c5981156a359f690923840d2627c9a6f.tar.gz |
- 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
-rw-r--r-- | doc/build/changelog/changelog_11.rst | 13 | ||||
-rw-r--r-- | doc/build/changelog/migration_11.rst | 38 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/array.py | 15 | ||||
-rw-r--r-- | test/dialect/postgresql/test_types.py | 26 |
4 files changed, 91 insertions, 1 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 824623421..2f49a6bdb 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -22,6 +22,19 @@ :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 367f20e62..21c976589 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 ebdfe1695..b88f139de 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 7aad23255..6ed90c76d 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 |