Skip to content

Skip fieldless custom types when exporting to SQL Server - #1146

Merged
1ilit merged 1 commit into
drawdb-io:mainfrom
NgoQuocViet2001:fix-mssql-fieldless-type
Sep 17, 2026
Merged

1ilit merged 1 commit into
drawdb-io:mainfrom
NgoQuocViet2001:fix-mssql-fieldless-type

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown
Contributor

Problem

tablesToSQLServer guards the first-field access with a comparison that can never be true:

}CREATE TYPE [${type.name}] FROM ${
  type.fields.length < 0
    ? ""
    : `${getTypeString(type.fields[0], obj.database, DB.MSSQL, true)}`
};\nGO\n`;

Array.prototype.length is never negative, so the else branch always runs. For a custom type with no fields, type.fields[0] is undefined, and getTypeString's MSSQL branch starts with let type = field.type;.

How it is reached

A new custom type starts out exactly like that — addType() in src/context/TypesContext.jsx creates it with no starter field:

{ id, name: `type_${prev.length}`, fields: [], comment: "" }

So opening the Types tab, adding a type, and choosing Export source → MSSQL before giving it a field is enough. Exporting a generic diagram with that type:

export THREW: TypeError: Cannot read properties of undefined (reading 'type')

In the editor this surfaces as an export that silently produces nothing, since the menu handler calls jsonToSQLServer without a try/catch.

Fix

Drop fieldless types before mapping, and remove the dead ternary the filter replaces. CREATE TYPE ... FROM needs a base type, and a fieldless type has none to offer, so there is nothing valid to emit for it.

Test plan

  • Ran jsonToSQLServer (bundled with esbuild, since the module uses extensionless imports) against a generic diagram containing one table, one fieldless type and one type with a DECIMAL field:
    • before: throws the TypeError above
    • after: emits CREATE TYPE [money_t] FROM DECIMAL; for the populated type, skips the empty one, and the table follows unchanged
  • Ran: npm run lint → clean.
  • There is no test suite in the repo to extend.

type.fields.length < 0 can never be true -- Array length is unsigned -- so the
guard meant to protect the type.fields[0] access was dead and the else branch
always ran. A custom type with no fields passed undefined into getTypeString,
whose MSSQL branch reads field.type, and the export threw.

A new type starts out that way: addType() creates it with fields: [], so
opening the Types tab and exporting to SQL Server before adding a field is
enough. Drop those types instead -- CREATE TYPE has nothing to derive from
without a field.
@vercel

vercel Bot commented Sep 14, 2026

Copy link
Copy Markdown

@NgoQuocViet2001 is attempting to deploy a commit to the dottle's projects Team on Vercel.

A member of the Team first needs to authorize it.

@danielgwilson

Copy link
Copy Markdown

Humanish project operator check (automated): I exercised File → Export SQL → MSSQL on local production builds of ca7ba9a and b5c7099, including the actual Export/download button.

  • Empty custom type only: base raises the reported TypeError and leaves Export disabled. Head downloads a .sql file containing two newlines, with no invalid CREATE TYPE statement.
  • Empty type + populated DECIMAL type + table: base fails the same way. Head downloads SQL that omits the empty type while retaining the populated alias, table/column comments, both columns, identity/default constraints, primary key and index. I compared the complete 321-byte download with the expected SQL.

Both production builds passed using their committed lockfiles. The requested base/head also differ in the js-yaml lock entry (4.3.2/4.3.1). This checks the browser export path; I did not execute the SQL against SQL Server.

@vercel

vercel Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
drawdb Ready Ready Preview Sep 17, 2026 7:05pm UTC

@1ilit 1ilit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you!

@1ilit
1ilit merged commit 1e6349e into drawdb-io:main Sep 17, 2026
4 checks passed

This branch was successfully deployed

1 active deployment
Preview — b5c7099d Deployed Sep 17, 2026 by vercel[bot]
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.

3 participants