Pogchamp-company/alembic-postgresql-enum

Generated TableReference is missing server_default

Closed this issue · 1 comments

Denhai commented
class MyEnum(enum.Enum):
    one = 1
    two = 2
    three = 3
    four = 4 # added


class ExampleTable(BaseModel):
    __tablename__ = "example_table"
    test_field = Column(Integer, primary_key=True, autoincrement=False)
    enum_field = Column(postgresql.ENUM(MyEnum), server_default=MyEnum.one.name)

Generated migration:

def upgrade() -> None:
    op.sync_enum_values(
        "public",
        "myenum",
        ["one", "two", "three", "four"],
        [
            TableReference(
                table_schema="public",
                table_name="example_table",
                column_name="enum_field",
                # should have:
                # existing_server_default="'one'::myenum"
            )
        ],
        enum_values_to_rename=[],
    )

Then when running the upgrade:

sqlalchemy.exc.ProgrammingError: (psycopg.errors.DatatypeMismatch) default for column "enum_field" cannot be cast automatically to type myenum
[SQL: ALTER TABLE public.example_table 
                ALTER COLUMN enum_field TYPE public.myenum 
                USING enum_field::text::public.myenum
                ]
Denhai commented

I thought this could be because the table_schema variable isn't used here?

table_schema = table.schema or default_schema
column_default = get_column_default(connection, table.schema, table.name, column.name)