Fix duplicate PRIMARY KEY on composite primary keys#26
Merged
CollierKing merged 2 commits intoJun 21, 2026
Conversation
A composite (multi-column) primary key was emitted both inline on the first column (get_column_specification, when first_pk=True) and as a table-level constraint (create_table_constraints, for len > 1), producing two PRIMARY KEY clauses. Cloudflare D1 rejects this with "more than one primary key" (SQLITE_ERROR), so a CREATE TABLE with a composite PK fails. Inline PRIMARY KEY only for single-column keys; composite keys are emitted solely as the table-level constraint, which create_table_constraints already handles. Adds a regression test (the existing PK tests only cover single-column keys). Signed-off-by: dbczumar <corey.zumar@databricks.com>
Owner
|
Thanks for the PR! Added a follow-up commit with live integration coverage for this fix on both connection paths:
Validation run locally:
|
Owner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A composite (multi-column) primary key compiles to two
PRIMARY KEYclauses, which Cloudflare D1 rejects:
get_column_specificationinlinesPRIMARY KEYfor the first PK column wheneverfirst_pk=True, whilecreate_table_constraintsalso emits the table-levelconstraint for composite keys — so composite PKs get both. Single-column keys
are fine (the table-level one is suppressed), which is why the existing tests
didn't catch it.
Fix
Inline
PRIMARY KEYonly for a single-column key; composite keys are emittedsolely as the table-level constraint (exactly what
create_table_constraintsalready intends). One guarded condition in
get_column_specification.After:
Single-column PKs are unchanged (
id TEXT NOT NULL PRIMARY KEY).Tests
Adds
test_create_table_composite_primary_key(the existing PK tests only coversingle-column keys). Full unit suite green (21 passed). Verified end to end
against a live D1 instance: the table now creates successfully.
Found while running Alembic migrations for a real app on D1. Two related
reflection gaps I hit in the same effort, in case they're useful as follow-ups
(happy to send PRs):
get_foreign_keysomits thereferred_schemakey thatSQLAlchemy's reflection accesses (raises
KeyErrorwhen reflecting a table withan FK), and
get_unique_constraintsisn't implemented (Alembic'sbatch_alter_tablecalls it). Both are reflection-only and orthogonal to thisDDL fix.