{"record":{"id":"2e2c729b44039399","repo":"SonarSource/sonarqube","slug":"cannot-find-sequence-for-table-s-on-column-s","errorCode":null,"errorMessage":"Cannot find sequence for table '%s' on column '%s'","messagePattern":"Cannot find sequence for table '(.+?)' on column '(.+?)'","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DbPrimaryKeyConstraintFinder.java","lineNumber":124,"sourceCode":"      \"WHERE table_name = UPPER('%s') \" +\n      \"AND constraint_type='P'\", tableName);\n  }\n\n  private static String getH2ConstraintQuery(String tableName) {\n    return format(\"SELECT constraint_name \"\n      + \"FROM information_schema.table_constraints \"\n      + \"WHERE table_name = '%s' and constraint_type = 'PRIMARY KEY'\", tableName.toUpperCase(Locale.ENGLISH));\n  }\n\n  // FIXME:: this method should be moved somewhere else\n  String getPostgresSqlSequence(String tableName, String columnName) throws SQLException {\n    try (Connection connection = db.getDataSource().getConnection();\n      PreparedStatement pstmt = connection.prepareStatement(format(\"SELECT pg_get_serial_sequence('%s', '%s')\", tableName, columnName));\n      ResultSet rs = pstmt.executeQuery()) {\n      if (rs.next()) {\n        return rs.getString(1);\n      }\n      throw new IllegalStateException(format(\"Cannot find sequence for table '%s' on column '%s'\", tableName, columnName));\n    }\n  }\n\n}\n","sourceCodeStart":106,"sourceCodeEnd":129,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DbPrimaryKeyConstraintFinder.java#L106-L129","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Confirm the column actually has a sequence via SELECT pg_get_serial_sequence('<table>','<col>'); only call this lookup for serial/identity columns.","Ensure the table exists in the current schema/search_path and the name matches exactly (lowercase unless quoted).","Adjust migration code to skip sequence handling when no sequence exists."],"exampleFix":"// before\nString seq = finder.getPostgresSqlSequence(\"issues\", \"not_autoincrement_id\");\n// after\nif (finder.getPostgresSqlSequence(\"issues\", \"id\") != null) { /* handle sequence */ }","handlingStrategy":"validation","validationCode":"SELECT pg_get_serial_sequence('issues','id'); -- call first; if NULL, skip sequence handling","typeGuard":null,"tryCatchPattern":"try { seq = finder.getPostgresSqlSequence(table, col); } catch (IllegalStateException e) { /* treat as 'no sequence' and continue */ }","preventionTips":["Only look up sequences for serial/identity columns","Match PostgreSQL identifier casing exactly (usually lowercase)","Verify table exists in the active schema/search_path"],"tags":["database","postgresql","sequence","primary-key"],"backgroundTag":"resource-not-found","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}