Skip to content

fix(export): emit the comma before inline foreign keys on SQLite - #1114

Merged
1ilit merged 1 commit into
drawdb-io:mainfrom
ntdatt812:fix/sqlite-fk-without-primary-key
Aug 23, 2026
Merged

1ilit merged 1 commit into
drawdb-io:mainfrom
ntdatt812:fix/sqlite-fk-without-primary-key

Conversation

@ntdatt812

Copy link
Copy Markdown
Contributor

Defect

A SQLite table that has a foreign key but no primary key exports as invalid SQL.

Repro — post.author_id references author.id, and post has no primary key:

CREATE TABLE IF NOT EXISTS "post" (
	"author_id" INTEGER	FOREIGN KEY ("author_id") REFERENCES "author"("id")
	ON UPDATE NO ACTION ON DELETE NO ACTION
);

sqlite3 rejects it:

near "FOREIGN": syntax error

Cause

The separator that precedes the inline FOREIGN KEY block is nested inside the primary-key branch:

table.fields.filter((f) => f.primary).length > 0
  ? `,\n\tPRIMARY KEY(...)${inlineFK !== "" ? ",\n" : ""}`
  : ""                                    // ← no primary key: no separator, even when inlineFK is non-empty
}${inlineFK}

So the ,\n is only emitted when the table happens to have a primary key. With a primary key the output is correct, which is why this isn't more visible — but a foreign key on a table without a declared primary key is ordinary: a join table, or any child row keyed by something other than a PK.

Fix

Move the separator out of the primary-key branch and key it on the foreign-key block itself:

}${inlineFK !== "" ? ",\n" : ""}${inlineFK}

Both SQLite export paths had the same construct, so both are fixed:

  • src/utils/exportSQL/sqlite.js — toSqlite
  • src/utils/exportSQL/generic.js — jsonToSQLite

Other dialects are unaffected: MySQL, MariaDB, Postgres, MSSQL and Oracle emit foreign keys as separate ALTER TABLE statements rather than inline.

Verification

There's no test harness in the repo, so I drove the real export functions directly and fed the output to an actual SQLite engine (bun:sqlite):

before after
FK, no PK near "FOREIGN": syntax error accepted
FK + PK (control) accepted accepted, byte-identical output

Verified on both toSqlite and jsonToSQLite. Output for tables that do have a primary key is unchanged.

npx eslint clean on both changed files.

A SQLite table with a foreign key but no primary key exported as invalid
SQL. The separator before the inline FOREIGN KEY block was nested inside
the primary-key branch, so it was only emitted when the table happened to
have a primary key:

  CREATE TABLE IF NOT EXISTS "post" (
  	"author_id" INTEGER	FOREIGN KEY ("author_id") REFERENCES "author"("id")

sqlite3 rejects that with `near "FOREIGN": syntax error`. Foreign keys on
a table without a primary key are ordinary — a join table, or any child
row keyed by something other than a declared PK.

Moving the separator out of the primary-key branch and keying it on the
foreign-key block itself fixes both SQLite export paths: `toSqlite` and
`jsonToSQLite`. Output for tables that do have a primary key is byte for
byte unchanged.
@vercel

vercel Bot commented Aug 18, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
drawdb Ready Ready Preview Aug 23, 2026 8:01pm

@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 8305412 into drawdb-io:main Aug 23, 2026
4 checks passed

This branch was successfully deployed

1 active deployment
Preview — 136e9494 Deployed Aug 23, 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.

2 participants