Skip to content

Script parser: honour column-level constraints and non-null primary keys - #25

Merged
SimonCropp merged 1 commit into
mainfrom
fix/script-column-constraints
Oct 9, 2026
Merged

SimonCropp merged 1 commit into
mainfrom
fix/script-column-constraints

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

Two fixes to how ScriptParser reads a SQL script. Diagrams read from a live database through SchemaReader were already correct; these bring the script path in line.

Column-level constraints were ignored

Only table-level constraints were processed, so anything declared on the column itself was dropped:

create table Parent (Id int primary key);
create table Child (Id int primary key, ParentId int not null references Parent(Id));

rendered both Id columns without pk, and with no Parent ||--o{ Child relationship. Column constraints now go through the same handling as table constraints, for both create table and alter table ... add. Named and unnamed forms both work.

Table-level primary key columns rendered as nullable

create table Parent (Id int, primary key(Id));

rendered int(nullable) Id pk. A primary key column is never nullable, so TableBuilder now marks every primary key column as not null when the table is built, however the key was declared. That replaces the inline-only check that was in ScriptParser.IsNullable.

Tests

  • New RenderMarkdownFromScriptWithColumnLevelConstraints covers a named inline primary key, a primary key column added by alter table, and named and unnamed inline foreign keys.
  • 12 existing snapshots change. In 11 the only difference is an added pk on inline primary key columns. In RenderMarkdownFromScriptWithTableLevelPrimaryKey, Child.Id gains pk and Parent.Id changes from int(nullable) Id pk to int Id pk.
  • Two of the changed snapshots are in SqlServerToMermaidTool.Tests, which CI does not run; they pass locally.

A primary key or foreign key declared on a column (Id int primary key, ParentId int references Parent(Id)) was ignored when reading a SQL script, so the column had no pk marker and the relationship was missing. Column-level constraints are now processed for both create table and alter table add.

A primary key column is never nullable, so a table-level primary key(Id) no longer renders its column as (nullable).
@SimonCropp SimonCropp added this to the 2.2.0 milestone Oct 9, 2026
@SimonCropp
SimonCropp merged commit 94c8cde into main Oct 9, 2026
4 checks passed
@SimonCropp
SimonCropp deleted the fix/script-column-constraints branch October 9, 2026 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant