Skip to content

fix: suppress redundant UNIQUE on primary key fields in SQL exports#998

Open
20syldev wants to merge 1 commit intodrawdb-io:mainfrom
20syldev:fix/unique-on-primary-key
Open

fix: suppress redundant UNIQUE on primary key fields in SQL exports#998
20syldev wants to merge 1 commit intodrawdb-io:mainfrom
20syldev:fix/unique-on-primary-key

Conversation

@20syldev
Copy link
Copy Markdown
Contributor

Summary

  • PRIMARY KEY already implies uniqueness in all SQL databases (MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, Oracle)
  • When a field had both primary: true and unique: true, the export was emitting UNIQUE on the column definition and a PRIMARY KEY(...) clause at the end of the CREATE TABLE, resulting in a redundant duplicate index
  • Fix: suppress the UNIQUE keyword when the field is already marked as primary in all SQL exporters (mysql.js, mariadb.js, postgres.js, mssql.js, sqlite.js, oraclesql.js, generic.js)

Root cause

Template files (template1.js through template6.js) set unique: true on primary key fields. The SQL export logic didn't account for this redundancy.

Test plan

  • Create a table in MySQL/MariaDB mode with a primary key
  • Export to SQL and verify the primary key column does not have UNIQUE in its column definition
  • Verify the PRIMARY KEY(...) clause is still present at the end of CREATE TABLE
  • Test with PostgreSQL, SQLite, MSSQL, and Oracle exports

Fixes #961

PRIMARY KEY already implies uniqueness in all SQL databases.
Emitting UNIQUE on a column that is also part of the PRIMARY KEY
creates a redundant duplicate index, impacting performance.

Fixes drawdb-io#961
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 20, 2026

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

A member of the Team first needs to authorize it.

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.

[BUG] Unnecessary Unique Index on Primary key is auto-selected in MYSQL & MariaDB

1 participant