无法移除Postgres ENUM

时间:2017-10-26 20:15:30

标签: python postgresql sqlalchemy

我在postgres中有一个枚举。它是通过sql alchemy定义的,如:

sa.Column('state', postgresql.ENUM(u'ACTIVE', u'STOPPED', u'FAILED', name='ProcessState'), nullable=True),

我无法删除它,但它似乎也不存在:

database=# select NULL :: ProcessState;
ERROR:  type "processstate" does not exist
LINE 1: select NULL :: ProcessState
                       ^
database=# SELECT n.nspname AS "schema", t.typname
     , string_agg(e.enumlabel, '|' ORDER BY e.enumsortorder) AS enum_labels
FROM   pg_catalog.pg_type t 
JOIN   pg_catalog.pg_namespace n ON n.oid = t.typnamespace 
JOIN   pg_catalog.pg_enum e ON t.oid = e.enumtypid  
GROUP  BY 1,2;
 schema |  typname  |      enum_labels      
--------+-----------+-----------------------
 public | ProcessState | ACTIVE|STOPPED|FAILED
(1 row)


database=# drop type public.ProcessState;
ERROR:  type "public.processstate" does not exist
database=# drop type ProcessState;
ERROR:  type "processstate" does not exist

我不知所措。它似乎存在并且不存在于同一时间,我无法删除它,或重新添加它。使用SQL Alchemy重新添加会导致错误:

File "python2.7/site-packages/sqlalchemy/engine/default.py", line 462, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) type "ProcessState" already exists
 [SQL: 'CREATE TYPE "ProcessState" AS ENUM (\'ACTIVE\', \'STOPPED\', \'FAILED\')']

任何想法是怎么回事?任何想法如何解决这一问题?我希望能够可靠地添加此类型并将其删除(无需删除整个数据库)。

1 个答案:

答案 0 :(得分:3)

看起来SQLAlchemy使用引号将此类型写入数据库(请参阅错误消息中的SQL)。

尝试:

DROP TYPE "ProcessState";