【问题标题】:Create unique index with SQLAlchemy on Postgres using MD5使用 MD5 在 Postgres 上使用 SQLAlchemy 创建唯一索引
【发布时间】:2020-05-12 22:14:02
【问题描述】:

我有一列可以包含长文本,我想在该列上创建唯一索引。

我在几个地方(例如here)读到,这样做的方法是在列的 MD5 上创建索引。

我尝试添加以下代码:

__table_args__ = (
        sa.Index('unique_data_hash', 'MD5(data)', unique=True)
    )

但是当我尝试使用它时,我得到了

KeyError: 'MD5(数据)'

如何使用 SQLAlchemy 添加此类索引?

【问题讨论】:

    标签: postgresql sqlalchemy unique-constraint unique-index


    【解决方案1】:

    经过另一次搜索,我意识到答案是使用 sa.func.md5,它就像一个魅力!

    【讨论】:

    • 你能使用任何其他的散列函数吗? MD5 在哈希冲突方面没有最好的记录。 @aviad-nissel
    猜你喜欢
    • 2015-03-14
    • 2013-09-11
    • 2018-02-13
    • 2017-07-12
    • 1970-01-01
    • 2014-08-21
    • 2015-09-02
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多