Skip to content

fix(postgres): support UNIQUE NULLS NOT DISTINCT constraint syntax#839

Merged
NQPhuc merged 2 commits intoholistics:masterfrom
frenzzy:fix/unique-nulls-not-distinct
Mar 9, 2026
Merged

fix(postgres): support UNIQUE NULLS NOT DISTINCT constraint syntax#839
NQPhuc merged 2 commits intoholistics:masterfrom
frenzzy:fix/unique-nulls-not-distinct

Conversation

@frenzzy
Copy link
Contributor

@frenzzy frenzzy commented Mar 8, 2026

Summary

Fix sql2dbml crash when parsing PostgreSQL dumps containing UNIQUE NULLS NOT DISTINCT constraints (introduced in PostgreSQL 15).

Root cause: The constraintelem grammar rule was missing the optional nulls_distinct? clause before the column list in the UNIQUE alternative. This caused the ANTLR parser to produce a null node for the column list, leading to Cannot read properties of null (reading 'accept') in visitConstraintelem.

Fix: Added nulls_distinct? (which already existed in the grammar for CREATE INDEX) to the UNIQUE branch of constraintelem. The NULLS NOT DISTINCT modifier is gracefully ignored in the DBML output, falling back to a regular UNIQUE constraint — consistent with how DBML currently handles index-level NULLS NOT DISTINCT.

Fixes #838

Changes

  • PostgreSQLParser.g4: Added nulls_distinct? to the UNIQUE branch of constraintelem (1-line change)
  • Regenerated PostgreSQLParser.js and PostgreSQLParser.interp via ANTLR 4.13.2
  • Added test fixture (nulls_not_distinct.in.sql / nulls_not_distinct.out.dbml)

Test plan

  • All 109 existing importer tests pass
  • All 237 dbml-core tests pass
  • New nulls_not_distinct test case passes
  • Verified with real-world PostgreSQL 18 pg_dump output containing UNIQUE NULLS NOT DISTINCT

PostgreSQL 15 introduced `NULLS NOT DISTINCT` for unique constraints,
allowing NULL values to be treated as equal. The parser grammar was
missing this optional clause in the `constraintelem` rule, causing a
crash when importing SQL dumps containing this syntax.

Fixes holistics#838

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@frenzzy
Copy link
Contributor Author

frenzzy commented Mar 8, 2026

While waiting for this PR to be merged, I've published a temporary fork to npm:

Packages:

Version: 6.4.0-nulls-not-distinct.0
Tag: nulls-not-distinct

Usage

# Using npx
npx -p @frenzzy/dbml-cli@nulls-not-distinct sql2dbml schema.sql --postgres -o schema.dbml

# Or install as dependency
npm install @frenzzy/dbml-cli@nulls-not-distinct

These are drop-in replacements until the official packages are updated.

@TeaNguyen TeaNguyen requested review from NQPhuc and hdnax March 9, 2026 02:59
@NQPhuc NQPhuc added the PR: Bug Fix 🐛 A type of pull request used for changelog categories label Mar 9, 2026
Copy link
Contributor

@NQPhuc NQPhuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frenzzy Thank you for your contribution. LGTM!

@NQPhuc NQPhuc merged commit dea2a4e into holistics:master Mar 9, 2026
1 of 3 checks passed
@frenzzy frenzzy deleted the fix/unique-nulls-not-distinct branch March 9, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql2dbml crashes on PostgreSQL UNIQUE NULLS NOT DISTINCT constraint

2 participants