{"record":{"id":"08fe38c26b96b79b","repo":"hibernate/hibernate-orm","slug":"no-add-primary-key-syntax-supported-by-sqlitediale","errorCode":null,"errorMessage":"No add primary key syntax supported by SQLiteDialect","messagePattern":"No add primary key syntax supported by SQLiteDialect","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLiteDialect.java","lineNumber":512,"sourceCode":"\n\t@Override\n\tpublic String getDropForeignKeyString() {\n\t\tthrow new UnsupportedOperationException( \"No drop foreign key syntax supported by SQLiteDialect\" );\n\t}\n\n\t@Override\n\tpublic String getAddForeignKeyConstraintString(\n\t\t\tString constraintName,\n\t\t\tString[] foreignKey,\n\t\t\tString referencedTable,\n\t\t\tString[] primaryKey,\n\t\t\tboolean referencesPrimaryKey) {\n\t\tthrow new UnsupportedOperationException( \"No add foreign key syntax supported by SQLiteDialect\" );\n\t}\n\n\t@Override\n\tpublic String getAddPrimaryKeyConstraintString(String constraintName) {\n\t\tthrow new UnsupportedOperationException( \"No add primary key syntax supported by SQLiteDialect\" );\n\t}\n\n\t@Override\n\tpublic boolean supportsCommentOn() {\n\t\treturn true;\n\t}\n\n\t@Override\n\tpublic boolean supportsIfExistsBeforeTableName() {\n\t\treturn true;\n\t}\n\n\t@Override\n\tpublic boolean doesReadCommittedCauseWritersToBlockReaders() {\n\t\t// TODO Validate (WAL mode...)\n\t\treturn true;\n\t}\n","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLiteDialect.java#L494-L530","documentation":"getAddPrimaryKeyConstraintString(...) on SQLiteDialect unconditionally throws because SQLite cannot add a primary key with ALTER TABLE; a PK is only definable as a column/table constraint in CREATE TABLE. Hibernate's schema tooling reaches this method when it needs to reconcile a primary key after the table already exists (for example an id mapping was added or changed and hbm2ddl update runs). The fix is always structural: rebuild the table, do not alter it.","triggerScenarios":"Running hbm2ddl update after adding/changing @Id or @EmbeddedId on an entity mapped to an existing SQLite table; changing the id column or key type; custom tooling calling getAddPrimaryKeyConstraintString directly.","commonSituations":"Evolving identifier mappings on a live SQLite database; generation scripts written for MySQL that stage PK creation as a separate ALTER TABLE ADD PRIMARY KEY; CI profiles with ddl-auto=update over a persisted SQLite file.","solutions":["Recreate the table with PRIMARY KEY inline (create new, copy, drop, rename) via a versioned migration","Fix the mapping so the PK exists from the first CREATE TABLE and use ddl-auto=create for fresh databases or none afterwards","Never emit ALTER TABLE ADD PRIMARY KEY for SQLite in custom DDL generators; guard on dialect type"],"exampleFix":"// before - update tries ALTER TABLE ADD PRIMARY KEY (throws)\n// hibernate.hbm2ddl.auto = update\n\n// after - one-time migration rebuilds the table\n// hibernate.hbm2ddl.auto = none\n// V3__orders_pk.sql:\n// CREATE TABLE orders_new(id integer PRIMARY KEY, ...);\n// INSERT INTO orders_new SELECT * FROM orders;\n// DROP TABLE orders;\n// ALTER TABLE orders_new RENAME TO orders;","handlingStrategy":"validation","validationCode":"if (dialect instanceof SQLiteDialect) {\n    // PK must be inline in CREATE TABLE; refuse to generate ALTER ADD PK\n    throw new IllegalStateException(\n        'Primary key change on SQLite requires a table rebuild migration, not ALTER TABLE');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Freeze @Id mappings before a SQLite database is provisioned","Check dialect type in DDL generators before emitting ALTER TABLE ADD PRIMARY KEY","Keep a recreate-table migration template for key changes"],"tags":["hibernate","sqlite","dialect","schema-migration","primary-key","ddl","alter-table"],"backgroundTag":"schema-migration-unsupported-constraint","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}