在 sqlalchemy 中删除具有 ENUM 类型的列的 table

Deleting a table having column having ENUM type in sqlalchemy

我在 postgres 数据库的 sqlalchemy 中有一个 table。它有一个依赖于 sqlalchemy.dialects.postgresql.ENUM 的列。当我尝试删除 table 时,出现错误。 DETAIL: column my_column of table myTable depends on type my_enum HINT: Use DROP ... CASCADE to drop the dependent objects too. 为什么会出现此错误以及如何解决?

from sqlalchemy import create_engine, Table, Column, MetaData
from sqlalchemy.dialects.postgresql import ENUM
mt = MetaData()
engine = create_engine('postgresql+psycopg2://localhost:5555/postgres')
mt.bind = engine
_type = ENUM('a', 'b', name = 'my_enum')
t = Table('myTable', mt, Column('my_column', _type))
t.create()
t.drop()

ENUM 类型随 table 一起删除。为了防止它发生,它必须与元数据对象相关联。所以,改变线路
_type = ENUM('a', 'b', name = 'my_enum', metadata = mt)
问题解决。
详情请参考https://www.kite.com/python/docs/sqlalchemy.dialects.postgresql.ENUM