SonarSource/sonarqube · error · java.lang.IllegalStateException

Cannot find sequence for table '%s' on column '%s'

Error message

Cannot find sequence for table '%s' on column '%s'

What it means

Thrown by DbPrimaryKeyConstraintFinder.getPostgresSqlSequence when pg_get_serial_sequence returns no row, meaning the given table column has no associated serial/identity sequence in PostgreSQL. This is used before dropping/renaming PK constraints.

Source

Thrown at server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DbPrimaryKeyConstraintFinder.java:124

      "WHERE table_name = UPPER('%s') " +
      "AND constraint_type='P'", tableName);
  }

  private static String getH2ConstraintQuery(String tableName) {
    return format("SELECT constraint_name "
      + "FROM information_schema.table_constraints "
      + "WHERE table_name = '%s' and constraint_type = 'PRIMARY KEY'", tableName.toUpperCase(Locale.ENGLISH));
  }

  // FIXME:: this method should be moved somewhere else
  String getPostgresSqlSequence(String tableName, String columnName) throws SQLException {
    try (Connection connection = db.getDataSource().getConnection();
      PreparedStatement pstmt = connection.prepareStatement(format("SELECT pg_get_serial_sequence('%s', '%s')", tableName, columnName));
      ResultSet rs = pstmt.executeQuery()) {
      if (rs.next()) {
        return rs.getString(1);
      }
      throw new IllegalStateException(format("Cannot find sequence for table '%s' on column '%s'", tableName, columnName));
    }
  }

}

View on GitHub (pinned to 184c821202)

Solutions

  1. Confirm the column actually has a sequence via SELECT pg_get_serial_sequence('<table>','<col>'); only call this lookup for serial/identity columns.
  2. Ensure the table exists in the current schema/search_path and the name matches exactly (lowercase unless quoted).
  3. Adjust migration code to skip sequence handling when no sequence exists.

Example fix

// before
String seq = finder.getPostgresSqlSequence("issues", "not_autoincrement_id");
// after
if (finder.getPostgresSqlSequence("issues", "id") != null) { /* handle sequence */ }
Defensive patterns

Strategy: validation

Validate before calling

SELECT pg_get_serial_sequence('issues','id'); -- call first; if NULL, skip sequence handling

Try / catch

try { seq = finder.getPostgresSqlSequence(table, col); } catch (IllegalStateException e) { /* treat as 'no sequence' and continue */ }

Prevention

When it happens

Trigger: Calling getPostgresSqlSequence for a column that is not auto-increment, or a table name that doesn't exist/isn't in the current schema search_path.

Common situations: Migrating a table whose PK is a natural key (no sequence); running against a schema where the table was created without serial columns; wrong table/column casing in PostgreSQL (case-sensitive identifiers).

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/2e2c729b44039399. Report an issue: GitHub.