apache/cassandra · error · InvalidRequestException

Cannot rename non PRIMARY KEY column

Error message

Cannot rename non PRIMARY KEY column %s

What it means

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.

Solutions

  1. For regular columns, add the new column, copy data with an UPDATE ... SET new = old, then drop the old column
  2. Rename only key columns, or recreate the table with the desired schema and migrate data
  3. Use INSERT INTO new_table SELECT-style migration if the table is small

Example fix

// before
ALTER TABLE users RENAME email TO contact;
// after (regular column)
ALTER TABLE users ADD contact text;
UPDATE users SET contact = email;
ALTER TABLE users DROP email;
Defensive patterns

Strategy: validation

Validate before calling

ColumnMetadata c = Schema.instance.getTableMetadata(ks, table).getColumn(oldName); if (c == null || !c.isPrimaryKeyColumn()) { /* use ADD+copy+DROP instead of RENAME */ }

Try / catch

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; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/81af5749ed84ca7d. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java:587

        private void renameColumn(KeyspaceMetadata keyspace,
                                  TableMetadata table,
                                  ColumnIdentifier oldName,
                                  ColumnIdentifier newName,
                                  boolean ifColumnsExists,
                                  TableMetadata.Builder tableBuilder,
                                  Views.Builder viewsBuilder)
        {
            ColumnMetadata column = table.getExistingColumn(oldName);
            if (null == column)
            {
                if (!ifColumnsExists)
                    throw ire("Column %s was not found in table %s", oldName, table);
                return;
            }

            if (!column.isPrimaryKeyColumn())
                throw ire("Cannot rename non PRIMARY KEY column %s", oldName);

            if (null != table.getColumn(newName))
            {
                throw ire("Cannot rename column %s to %s in table '%s'; another column with that name already exists",
                          oldName,
                          newName,
                          table);
            }

            if (!table.indexes.isEmpty())
                AlterTableStatement.validateIndexesForColumnModification(table, oldName, true);

            for (ViewMetadata view : keyspace.views.forTable(table.id))
            {
                if (view.includes(oldName))
                {
                    viewsBuilder.put(viewsBuilder.get(view.name()).withRenamedPrimaryKeyColumn(oldName, newName));
                }

View on GitHub (pinned to 88fd0f6a0e)