hibernate/hibernate-orm · error · UnsupportedOperationException
No add primary key syntax supported by SQLiteDialect
Error message
No add primary key syntax supported by SQLiteDialect
What it means
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.
Source
Thrown at hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLiteDialect.java:512
@Override
public String getDropForeignKeyString() {
throw new UnsupportedOperationException( "No drop foreign key syntax supported by SQLiteDialect" );
}
@Override
public String getAddForeignKeyConstraintString(
String constraintName,
String[] foreignKey,
String referencedTable,
String[] primaryKey,
boolean referencesPrimaryKey) {
throw new UnsupportedOperationException( "No add foreign key syntax supported by SQLiteDialect" );
}
@Override
public String getAddPrimaryKeyConstraintString(String constraintName) {
throw new UnsupportedOperationException( "No add primary key syntax supported by SQLiteDialect" );
}
@Override
public boolean supportsCommentOn() {
return true;
}
@Override
public boolean supportsIfExistsBeforeTableName() {
return true;
}
@Override
public boolean doesReadCommittedCauseWritersToBlockReaders() {
// TODO Validate (WAL mode...)
return true;
}
View on GitHub (pinned to fad1729dce)
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
Example fix
// before - update tries ALTER TABLE ADD PRIMARY KEY (throws) // hibernate.hbm2ddl.auto = update // after - one-time migration rebuilds the table // hibernate.hbm2ddl.auto = none // V3__orders_pk.sql: // CREATE TABLE orders_new(id integer PRIMARY KEY, ...); // INSERT INTO orders_new SELECT * FROM orders; // DROP TABLE orders; // ALTER TABLE orders_new RENAME TO orders;
Defensive patterns
Strategy: validation
Validate before calling
if (dialect instanceof SQLiteDialect) {
// PK must be inline in CREATE TABLE; refuse to generate ALTER ADD PK
throw new IllegalStateException(
'Primary key change on SQLite requires a table rebuild migration, not ALTER TABLE');
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- No add foreign key syntax supported by SQLiteDialect
- No drop foreign key syntax supported by SQLiteDialect
- SingleStore does not support altering primary key.
- Cannot add primary key constraint in Cloud Spanner.
- SingleStore does not support foreign keys and referential in
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/08fe38c26b96b79b.
Report an issue: GitHub.