{"record":{"id":"81af5749ed84ca7d","repo":"apache/cassandra","slug":"cannot-rename-non-primary-key-column-s","errorCode":null,"errorMessage":"Cannot rename non PRIMARY KEY column %s","messagePattern":"Cannot rename non PRIMARY KEY column (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java","lineNumber":587,"sourceCode":"\n        private void renameColumn(KeyspaceMetadata keyspace,\n                                  TableMetadata table,\n                                  ColumnIdentifier oldName,\n                                  ColumnIdentifier newName,\n                                  boolean ifColumnsExists,\n                                  TableMetadata.Builder tableBuilder,\n                                  Views.Builder viewsBuilder)\n        {\n            ColumnMetadata column = table.getExistingColumn(oldName);\n            if (null == column)\n            {\n                if (!ifColumnsExists)\n                    throw ire(\"Column %s was not found in table %s\", oldName, table);\n                return;\n            }\n\n            if (!column.isPrimaryKeyColumn())\n                throw ire(\"Cannot rename non PRIMARY KEY column %s\", oldName);\n\n            if (null != table.getColumn(newName))\n            {\n                throw ire(\"Cannot rename column %s to %s in table '%s'; another column with that name already exists\",\n                          oldName,\n                          newName,\n                          table);\n            }\n\n            if (!table.indexes.isEmpty())\n                AlterTableStatement.validateIndexesForColumnModification(table, oldName, true);\n\n            for (ViewMetadata view : keyspace.views.forTable(table.id))\n            {\n                if (view.includes(oldName))\n                {\n                    viewsBuilder.put(viewsBuilder.get(view.name()).withRenamedPrimaryKeyColumn(oldName, newName));\n                }","sourceCodeStart":569,"sourceCodeEnd":605,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java#L569-L605","documentation":"Cassandra only permits renaming PRIMARY KEY columns (partition/clustering key parts). When renameColumn (AlterTableStatement.java:587) sees a non-key column, it throws this InvalidRequest, because renaming regular columns would require rewriting all data and is instead done via ADD/DROP column pairs.","triggerScenarios":"ALTER TABLE t RENAME some_regular_column TO other; where some_regular_column is not part of PRIMARY KEY. Checked immediately after the column is found to exist.","commonSituations":"Developers assuming RENAME works like other databases for value columns; trying to rename a static column that is not a key column; migrating SQL habits to CQL.","solutions":["For regular columns, add the new column, copy data with an UPDATE ... SET new = old, then drop the old column","Rename only key columns, or recreate the table with the desired schema and migrate data","Use INSERT INTO new_table SELECT-style migration if the table is small"],"exampleFix":"// before\nALTER TABLE users RENAME email TO contact;\n// after (regular column)\nALTER TABLE users ADD contact text;\nUPDATE users SET contact = email;\nALTER TABLE users DROP email;","handlingStrategy":"validation","validationCode":"ColumnMetadata c = Schema.instance.getTableMetadata(ks, table).getColumn(oldName); if (c == null || !c.isPrimaryKeyColumn()) { /* use ADD+copy+DROP instead of RENAME */ }","typeGuard":null,"tryCatchPattern":"try { session.execute(renameStmt); } catch (InvalidRequestException e) { if (e.getMessage().startsWith(\"Cannot rename non PRIMARY KEY column\")) { /* fall back to add/copy/drop migration */ } else throw e; }","preventionTips":["Only use RENAME for partition/clustering key columns","Document in migration tooling that value columns are renamed via ADD/UPDATE/DROP","Review schema DDL against the table definition before applying"],"tags":["cassandra","cql","schema","alter-table","rename"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}