{"record":{"id":"ade3b7ca2763716d","repo":"apache/cassandra","slug":"cannot-use-alter-table-on-a-materialized-view-use","errorCode":null,"errorMessage":"Cannot use ALTER TABLE on a materialized view; use ALTER MATERIALIZED VIEW instead","messagePattern":"Cannot use ALTER TABLE on a materialized view; use ALTER MATERIALIZED VIEW instead","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java","lineNumber":138,"sourceCode":"        Keyspaces schema = metadata.schema.getKeyspaces();\n        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n\n        TableMetadata table = null == keyspace\n                            ? null\n                            : keyspace.getTableOrViewNullable(tableName);\n\n        if (null == table)\n        {\n            if (!ifExists)\n                throw ire(\"Table '%s.%s' doesn't exist\", keyspaceName, tableName);\n            return schema;\n        }\n\n        if (table.params.pendingDrop)\n            throw ire(\"Cannot use ALTER TABLE on a table that is being dropped.\");\n\n        if (table.isView())\n            throw ire(\"Cannot use ALTER TABLE on a materialized view; use ALTER MATERIALIZED VIEW instead\");\n\n        return schema.withAddedOrUpdated(apply(metadata.nextEpoch(), keyspace, table, metadata));\n    }\n\n    SchemaChange schemaChangeEvent(KeyspacesDiff diff)\n    {\n        return new SchemaChange(Change.UPDATED, Target.TABLE, keyspaceName, tableName);\n    }\n\n    public void authorize(ClientState client)\n    {\n        client.ensureTablePermission(keyspaceName, tableName, Permission.ALTER);\n    }\n\n    @Override\n    public AuditLogContext getAuditLogContext()\n    {\n        return new AuditLogContext(AuditLogEntryType.ALTER_TABLE, keyspaceName, tableName);","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java#L120-L156","documentation":"AlterTableStatement.apply validation: the named table is actually a materialized view, and ALTER TABLE cannot alter views. The statement fails with InvalidRequestException; ALTER MATERIALIZED VIEW must be used instead.","triggerScenarios":"'ALTER TABLE ks.view_name ...' where the named table is actually a materialized view — e.g. views created via CREATE MATERIALIZED VIEW appear in the same namespace as tables and getTableOrViewNullable resolves them","commonSituations":"Altering a view by mistake because its name resembles the base table; automation enumerating all tables from system_schema.tables (views appear there too) and applying the same DDL; renaming refactors that swapped a table for a view.","solutions":["Use ALTER MATERIALIZED VIEW ks.view_name WITH ... to change view properties","If the intent was to change the base table, target the base table name instead","Remember view properties like gc_grace_seconds are changed on the view, not the base table"],"exampleFix":"// before\nsession.execute(\"ALTER TABLE my_app.events_by_day WITH gc_grace_seconds = 0\");\n// after\nsession.execute(\"ALTER MATERIALIZED VIEW my_app.events_by_day WITH gc_grace_seconds = 0\");","handlingStrategy":"validation","validationCode":"Row r = session.execute(\"SELECT kind FROM system_schema.tables WHERE keyspace_name=? AND table_name=?\", ks, name).one();\nif (r != null && \"view\".equals(r.getString(\"kind\")))\n    throw new IllegalArgumentException(name + \" is a materialized view; use ALTER MATERIALIZED VIEW\");","typeGuard":null,"tryCatchPattern":"try {\n    session.execute(alter);\n} catch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"materialized view\"))\n        session.execute(alter.replaceFirst(\"(?i)ALTER TABLE\", \"ALTER MATERIALIZED VIEW\"));\n    else throw e;\n}","preventionTips":["Resolve view vs table via system_schema.views / system_schema.tables before DDL","Exclude materialized views when enumerating 'tables' for bulk DDL","Name views distinctly from base tables to avoid mix-ups"],"tags":["cassandra","cql","ddl","materialized-view"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}