metadata.create_all do not create Enum in postgre ARRAY

Issue #2729 resolved
Former user created an issue

metadata.create_all do not create work_place_roles if Enum in sqlalchemy.dialects.postgresql.ARRAY

class WorkPlacement(Base):
    __tablename__ = 'work_placement'

    roles = Column(ARRAY(Enum('manager', 'place_admin', 'carwash_admin',
                                      'parking_admin', 'service_admin', 'tire_admin',
                                      'mechanic', 'carwasher', 'tire_mechanic', name="work_place_roles")))

i need do so:

class WorkPlacement(Base):
    __tablename__ = 'work_placement'


    tmp_role = Column(Enum('manager', 'place_admin', 'carwash_admin',
                                      'parking_admin', 'service_admin', 'tire_admin',
                                      'mechanic', 'carwasher', 'tire_mechanic', name="work_place_roles"))
    roles = Column(ARRAY(Enum(name="work_place_roles")))

Comments (9)

  1. Mike Bayer repo owner

    good catch. Here is a better workaround for now:

    from sqlalchemy import event
    
    Base = declarative_base()
    
    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))
    
    @event.listens_for(Base.metadata, "before_create")
    def _create_enum(metadata, conn, **kw):
        enum.create(conn, checkfirst=True)
    
  2. Mike Bayer repo owner

    part of the effort here though we still need to handle result rows

    diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py
    index 0c48ea8..1d2da8b 100644
    --- a/lib/sqlalchemy/sql/sqltypes.py
    +++ b/lib/sqlalchemy/sql/sqltypes.py
    @@ -1496,7 +1496,7 @@ class Interval(_DateAffinity, TypeDecorator):
             return self.impl.coerce_compared_value(op, value)
    
    
    -class Array(Indexable, Concatenable, TypeEngine):
    +class Array(SchemaEventTarget, Indexable, Concatenable, TypeEngine):
         """Represent a SQL Array type.
    
         .. note::  This type serves as the basis for all ARRAY operations.
    @@ -1735,6 +1735,19 @@ class Array(Indexable, Concatenable, TypeEngine):
         def compare_values(self, x, y):
             return x == y
    
    +    def _set_parent(self, column):
    +        """Support SchemaEventTarget"""
    +
    +        if isinstance(self.item_type, SchemaEventTarget):
    +            self.item_type._set_parent(column)
    +
    +    def _set_parent_with_dispatch(self, parent):
    +        """Support SchemaEventTarget"""
    +
    +        if isinstance(self.item_type, SchemaEventTarget):
    +            self.item_type._set_parent_with_dispatch(parent)
    +
    +
    
     class REAL(Float):
    
    diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py
    index f5ab1a8..2397efb 100644
    --- a/lib/sqlalchemy/sql/type_api.py
    +++ b/lib/sqlalchemy/sql/type_api.py
    @@ -774,13 +774,13 @@ class TypeDecorator(SchemaEventTarget, TypeEngine):
             return self.impl._type_affinity
    
         def _set_parent(self, column):
    -        """Support SchemaEentTarget"""
    +        """Support SchemaEventTarget"""
    
             if isinstance(self.impl, SchemaEventTarget):
                 self.impl._set_parent(column)
    
         def _set_parent_with_dispatch(self, parent):
    -        """Support SchemaEentTarget"""
    +        """Support SchemaEventTarget"""
    
             if isinstance(self.impl, SchemaEventTarget):
                 self.impl._set_parent_with_dispatch(parent)
    
  3. Mike Bayer repo owner
    • 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

    → <<cset 371f1a82c598>>

  4. Log in to comment