繁体   English   中英

使用 SQLAlchemy 和 MS SQL Server 实现 Unique if not Null 约束

[英]Implementing Unique if not Null Constraint with SQLAlchemy and MS SQL Server

我想对列强制执行唯一约束,但前提是它不为空。 这是 SQLite 中的默认行为,但不是 MS SQL Server 中的默认行为。

这似乎在SQL Server中实现这一点的方法是使用一个索引与where子句,如在这个岗位 我的问题是我如何在 SQLAlchemy 中做到这一点并让它仍然与 SQLite 一起使用。

我认为这大约是我想要的:

class MyClass(Base):
    __tablename__ = 'myclass'
    __table_args__ = (Index('unique_index', <where clause?>, unique=True)
    my_column = Column(Integer) # The column I want unique if not null

where <where clause?>是一些表达式,它将索引放在my_column ,它不是NULL 通过阅读索引文档,这似乎是可能的,但我不知道我需要什么表达式。 对此或不同方法的帮助将不胜感激。

使用过滤索引的SQL Server解决方案:

CREATE TABLE tab(col INT);
CREATE UNIQUE INDEX uq_tab_col ON tab(col) WHERE col IS NOT NULL;

INSERT INTO tab(col) VALUES (1),(2),(NULL),(NULL);

-- INSERT INTO tab(col) VALUES (1);
-- Cannot insert duplicate key row in object 'dbo.tab' with unique index 'uq_tab_col'.
-- The duplicate key value is (1).

SELECT * FROM tab;

LiveDemo

对于后来发现这一点的任何其他人。 SQL Alchemy 现在支持此索引文档

import sqlalchemy as sa

sa.Table(
    sa.Column('column', sa.String(50), nullable=True),
    sa.Index('uq_column_allows_nulls', mssql_where=sa.text('column IS NOT NULL'),
)

如果您打算像我使用此代码一样使用 alembic。

import sqlalchemy as sa
import alembic as op

op.create_index(
    name='uq_column_name',
    table_name='table',
    columns=['column'],
    mssql_where=sa.text('column IS NOT NULL'),
)

这使用sqlalchemysql 表达式文本和create_indexdialect_expression 关键字参数mssql_where=

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM