hibernate/hibernate-orm · error · SchemaExtractionException
Encountered primary keys differing name on table %s
Error message
Encountered primary keys differing name on table %s
What it means
Reading a single table's primary key via the JDBC metadata resultset, Hibernate expects every row for that table to repeat the same PK_NAME. When successive rows report different primary-key names, the metadata is self-inconsistent and extraction fails with SchemaExtractionException instead of building a wrong PrimaryKeyInformation.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/AbstractInformationExtractorImpl.java:948
private PrimaryKeyInformation extractPrimaryKeyInformation(TableInformation tableInformation, ResultSet resultSet)
throws SQLException {
final List<ColumnInformation> columns = new ArrayList<>();
boolean firstPass = true;
Identifier primaryKeyIdentifier = null;
while ( resultSet.next() ) {
final String currentPkName = resultSet.getString( getResultSetPrimaryKeyNameLabel() );
final Identifier currentPrimaryKeyIdentifier =
currentPkName == null ? null : toIdentifier( currentPkName );
if ( firstPass ) {
primaryKeyIdentifier = currentPrimaryKeyIdentifier;
firstPass = false;
}
else {
if ( !Objects.equals( primaryKeyIdentifier, currentPrimaryKeyIdentifier ) ) {
throw new SchemaExtractionException( "Encountered primary keys differing name on table "
+ tableInformation.getName().toString() );
}
}
final int columnPosition = resultSet.getInt( getResultSetColumnPositionColumn() );
final int index = columnPosition - 1;
// Fill up the array list with nulls up to the desired index, because some JDBC drivers don't return results ordered by column position
while ( columns.size() <= index ) {
columns.add( null );
}
final Identifier columnIdentifier =
toIdentifier( resultSet.getString( getResultSetColumnNameLabel() ) );
columns.set( index, tableInformation.getColumn( columnIdentifier ) );
}
if ( firstPass ) {
// we did not find any results (no pk)
return null;
}View on GitHub (pinned to fad1729dce)
Solutions
- Upgrade the JDBC driver — inconsistent PK_NAME for one table violates the JDBC contract.
- Qualify the mapping's catalog/schema so the resultset contains only the intended table's rows.
- Toggle hibernate.hbm2ddl.jdbc_metadata_extraction_strategy (grouped vs individually) to take a different metadata query path.
Defensive patterns
Strategy: try-catch
Try / catch
try {
schemaValidator.validate(metadata, databaseModel);
} catch (SchemaExtractionException e) {
if (e.getMessage() != null && e.getMessage().contains("primary keys differing name")) {
// driver metadata inconsistency — upgrade driver, then re-run
}
throw e;
} Prevention
- Pin a JDBC driver version validated in CI against the real database.
- Qualify catalog/schema on mappings to keep metadata resultsets single-table.
- Re-test schema validation after any driver upgrade.
When it happens
Trigger: Per-table primary-key extraction where the driver returns rows with differing PK_NAME for the same table: a driver defect, or a resultset that actually spans name-matching tables in other schemas because the driver ignored the schema filter.
Common situations: Older Oracle/MySQL/DB2 JDBC drivers; same-named tables in multiple schemas on case-normalizing databases; views or materialized views with unusual metadata; drivers after a major version change.
Related errors
- Primary Key information was missing for KEY_SEQ = %s
- Database doesn't support extracting all primary keys at once
- Could not locate table information for %s
- Primary Key information was missing for key [%s] on table [%
- No add primary key syntax supported by SQLiteDialect
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/d35ebf22491a6ee8.
Report an issue: GitHub.