Python 如何在SQLAlchemy中使用ExcludeConstraint中的强制转换?

Python 如何在SQLAlchemy中使用ExcludeConstraint中的强制转换?,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,在PostgreSQL中,我可以创建一个包含排除约束的表,该排除约束涉及CAST(CAST是必需的,因为UUID类型没有gist的默认运算符类): 我不知道如何在SQLAlchemy中完成同样的事情。我试过这个: from sqlalchemy import * from sqlalchemy.dialects.postgresql import ( UUID, INT4RANGE, ExcludeConstraint, TEXT ) class Example(Base):

在PostgreSQL中,我可以创建一个包含排除约束的表,该排除约束涉及
CAST
CAST
是必需的,因为
UUID
类型没有
gist
的默认运算符类):

我不知道如何在SQLAlchemy中完成同样的事情。我试过这个:

from sqlalchemy import *
from sqlalchemy.dialects.postgresql import (
    UUID, INT4RANGE, ExcludeConstraint, TEXT
)

class Example(Base):
    __tablename__ = 'example'
    id = Column(UUID)
    some_range = Column(INT4RANGE)
    __table_args__ = (
        ExcludeConstraint(
            (cast('id', TEXT), '='), ('some_range', '&&')
        ),
    )
但是我得到了错误
sqlalchemy.exc.ArgumentError:无法将未命名列添加到列集合

如何让SQLAlchemy使用
ExcludeConstraint
中的
CAST
?或者,如何使用原始SQL定义表上的排除约束?

从中,解决方法(对于1.0.6之前的版本)是

从中,解决方法(对于1.0.6之前的版本)是

from sqlalchemy import *
from sqlalchemy.dialects.postgresql import (
    UUID, INT4RANGE, ExcludeConstraint, TEXT
)

class Example(Base):
    __tablename__ = 'example'
    id = Column(UUID)
    some_range = Column(INT4RANGE)
    __table_args__ = (
        ExcludeConstraint(
            (cast('id', TEXT), '='), ('some_range', '&&')
        ),
    )
from sqlalchemy.sql.elements import quoted_name

    ExcludeConstraint(
        (Column(quoted_name('CAST("id" AS TEXT)', quote=False)), '='), ('some_range', '&&')
    ),