Before creating a new record I want to check a condition that the combination of native_linux_user
and is_active
is unique but is_active
must be True
. Multiple native_linux_user
with is_active=False
may exist, but only one native_linux_user
with is_active=True
can exist.
I tried to use a CheckConstraint
within a UniqueConstraint
like this, but it didn't work. How do I make this type of constraint?
__table_args__ = (
UniqueConstraint(
'native_linux_user',
CheckConstraint('is_active=True', name='active_check'),
name='_username_uniqueness'
),
)
From SQL side you can create partial
UNIQUE
index:And SQLAlchemy model with corresponding
Index
: