[Sqlalchemy-tickets] Issue #3832: _set_table not called in 1.1 on custom type based on TypeDecorato
Brought to you by:
zzzeek
From: Adrian <iss...@bi...> - 2016-10-20 12:26:53
|
New issue 3832: _set_table not called in 1.1 on custom type based on TypeDecorator & SchemaType https://bitbucket.org/zzzeek/sqlalchemy/issues/3832/_set_table-not-called-in-11-on-custom-type Adrian: We are using a custom class handling int-based enums (since 1.0 didn't have python-native enum support) with an autogenerated CHECK constraint. In 1.1 this constraint is not added anymore. In the [changelog entry](http://docs.sqlalchemy.org/en/rel_1_1/changelog/migration_11.html#typedecorator-now-works-with-enum-boolean-schema-types-automatically) regarding TypeDecorator I couldn't see any reason why _set_table would not be called anymore. ```python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql.sqltypes import SchemaType from enum import Enum Base = declarative_base() class PyIntEnum(TypeDecorator, SchemaType): impl = SmallInteger def __init__(self, enum=None, exclude_values=None): self.enum = enum self.exclude_values = set(exclude_values or ()) TypeDecorator.__init__(self) SchemaType.__init__(self) def process_bind_param(self, value, dialect): return int(value) if value is not None else None def process_result_value(self, value, dialect): pass # not relevant for DDL def _set_table(self, column, table): e = CheckConstraint(type_coerce(column, self).in_(x.value for x in self.enum if x not in self.exclude_values), 'valid_enum_{}'.format(column.name)) e.info['alembic_dont_render'] = True assert e.table is table class MyEnum(int, Enum): a = 1 b = 2 c = 3 class Foo(Base): __tablename__ = 'foo' id = Column(Integer, primary_key=True) enum = Column(PyIntEnum(MyEnum)) e = create_engine('postgresql:///test', echo=True) Base.metadata.drop_all(e) Base.metadata.create_all(e) ``` SQL emitted in 1.1: ```sql CREATE TABLE foo ( id SERIAL NOT NULL, enum SMALLINT, PRIMARY KEY (id) ) ``` SQL emitted in 1.0 ```sql CREATE TABLE foo ( id SERIAL NOT NULL, enum SMALLINT, PRIMARY KEY (id), CONSTRAINT valid_enum_enum CHECK (enum IN (1, 2, 3)) ) ``` |