Skip to content

Translate NULLS NOT DISTINCT into a companion unique index - #166

Merged
LucaCappelletti94 merged 3 commits into
mainfrom
upstream/nulls-not-distinct-trigger
Oct 27, 2026
Merged

LucaCappelletti94 merged 3 commits into
mainfrom
upstream/nulls-not-distinct-trigger

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Collaborator

A unique constraint or unique index declared NULLS NOT DISTINCT was refused, because a SQLite unique index always keeps NULLs apart and dropping the clause would let the replica accept rows PostgreSQL rejects. A schema could therefore not declare a constraint its backend supports.

The plain unique key is still emitted, so foreign keys and ON CONFLICT targets find it, and a companion unique index named after it with _nnd appended encodes each key that can be NULL as (key IS NULL, ifnull(key, 0)), keeping the column's collation and a partial index's predicate. NULL keys then collide as an ordinary unique violation, which INSERT OR IGNORE and ON CONFLICT DO NOTHING skip as PostgreSQL does. A trigger raising on an IS match would have aborted those and surfaced as a trigger error. DROP INDEX drops the companion as well, read from the schema as it stood before the drop, and an unnamed NULLS NOT DISTINCT index is refused since its companion would have no name to follow.

ON CONFLICT (key) DO UPDATE still aborts on a repeated NULL key where PostgreSQL updates the row, since SQLite resolves that target to the plain index. The README lists it under semantic differences.

@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request rust Pull requests that update rust code labels Oct 27, 2026
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.00000% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/impls/translator_impls/nulls_not_distinct.rs 98.19% 3 Missing ⚠️
src/impls/translator_impls/create_index.rs 95.45% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@LucaCappelletti94
LucaCappelletti94 merged commit d483ae1 into main Oct 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request rust Pull requests that update rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant