1. Michael Bayer
  2. sqlalchemy
  3. Issues

Issues

Issue #2919 resolved

schematype support for typedecorator / PG ENUM

Michael Bayer
repo owner created an issue

figure out how users would know to follow this pattern, or how this can be transparent:

#!python
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects import postgresql


Base = declarative_base()

class MyEnum(sa.types.TypeDecorator, sa.types.SchemaType):
    impl = postgresql.ENUM

    def _set_parent(self, column):
        self.impl._set_parent(column)

class Test(Base):
    __tablename__ = 'test'

    id = sa.Column(sa.Integer, primary_key=True)
    enum_column = sa.Column(MyEnum('1', '2', '3', name='my_enum_type'))

engine = sa.create_engine('postgresql://scott:tiger@localhost/test', echo=True)
Base.metadata.create_all(engine)

Comments (5)

  1. Michael Bayer reporter
    #!diff
    
    diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py
    index b9826e5..9bfd6dc 100644
    --- a/lib/sqlalchemy/sql/type_api.py
    +++ b/lib/sqlalchemy/sql/type_api.py
    @@ -772,6 +772,11 @@ class TypeDecorator(TypeEngine):
             """
             return self.impl._type_affinity
    
    +    def _set_parent(self, column):
    +        """Support SchemaType"""
    +
    +        self.impl._set_parent(column)
    +
         def type_engine(self, dialect):
             """Return a dialect-specific :class:`.TypeEngine` instance
             for this :class:`.TypeDecorator`.
    
  2. Michael Bayer reporter
    • The :class:.TypeDecorator type extender will now work in conjunction with a :class:.SchemaType implementation, typically :class:.Enum or :class:.Boolean with regards to ensuring that the per-table events are propagated from the implementation type to the outer type. These events are used to ensure that the constraints or Postgresql types (e.g. ENUM) are correctly created (and possibly dropped) along with the parent table. fixes #2919

    → <<cset ed535649d423>>

  3. Log in to comment