{"record":{"id":"7256da4dd7721048","repo":"apache/cassandra","slug":"unknown-column-s-referenced-in-primary-key-for-7256da","errorCode":null,"errorMessage":"Unknown column '%s' referenced in PRIMARY KEY for materialized view '%s'","messagePattern":"Unknown column '(.+?)' referenced in PRIMARY KEY for materialized view '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":246,"sourceCode":"                       .map(table::getColumn)\n                       .filter(ColumnMetadata::isStatic)\n                       .findAny()\n                       .ifPresent(c -> { throw ire(\"Cannot include static column '%s' in materialized view '%s'\", c, viewName); });\n\n        /*\n         * Process PRIMARY KEY columns and CLUSTERING ORDER BY clause\n         */\n\n        if (partitionKeyColumns.isEmpty())\n            throw ire(\"Must provide at least one partition key column for materialized view '%s'\", viewName);\n\n        HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();\n\n        concat(partitionKeyColumns, clusteringColumns).forEach(name ->\n        {\n            ColumnMetadata column = table.getColumn(name);\n            if (null == column || !selectedColumns.contains(name))\n                throw ire(\"Unknown column '%s' referenced in PRIMARY KEY for materialized view '%s'\", name, viewName);\n\n            if (!primaryKeyColumns.add(name))\n                throw ire(\"Duplicate column '%s' in PRIMARY KEY clause for materialized view '%s'\", name, viewName);\n\n            AbstractType<?> type = column.type;\n\n            if (type.isMultiCell())\n            {\n                if (type.isCollection())\n                    throw ire(\"Invalid non-frozen collection type '%s' for PRIMARY KEY column '%s'\", type, name);\n                else\n                    throw ire(\"Invalid non-frozen user-defined type '%s' for PRIMARY KEY column '%s'\", type, name);\n            }\n\n            if (type.isCounter())\n                throw ire(\"counter type is not supported for PRIMARY KEY column '%s'\", name);\n\n            if (type.referencesDuration())","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L228-L264","documentation":"Every column named in the view's PRIMARY KEY must exist in the base table AND be present in the view's SELECT list. A name that is misspelled, doesn't exist, or was simply not projected is rejected here.","triggerScenarios":"CREATE MATERIALIZED VIEW ... PRIMARY KEY (typo_col, ...) or PRIMARY KEY references a column not in the SELECT list.","commonSituations":"Typos or case mismatches in quoted identifiers; forgetting to add a PRIMARY KEY column to the AS SELECT list; referencing a renamed/nonexistent column after schema evolution.","solutions":["Fix the column name to match an existing base table column (mind case sensitivity with quoted identifiers)","Add the column to the view's SELECT list and the corresponding IS NOT NULL restriction in WHERE","Verify the base table schema with DESCRIBE TABLE before authoring the view"],"exampleFix":"// before\nCREATE MATERIALIZED VIEW mv AS SELECT pk, ck FROM base WHERE pk IS NOT NULL AND ck IS NOT NULL PRIMARY KEY (pk, ckk);\n// after\nCREATE MATERIALIZED VIEW mv AS SELECT pk, ck FROM base WHERE pk IS NOT NULL AND ck IS NOT NULL PRIMARY KEY (pk, ck);","handlingStrategy":"validation","validationCode":"for (String c : primaryKeyCols) if (!baseTableColumns.contains(c) || !selectColumns.contains(c)) throw new IllegalArgumentException(\"Key column not in base table or select list: \" + c);","typeGuard":null,"tryCatchPattern":"try { session.execute(createMvStmt); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"Unknown column\")) { /* reconcile names, add to SELECT/WHERE */ } }","preventionTips":["Copy column names from DESCRIBE TABLE output, respecting case-sensitive quoting","Ensure every PRIMARY KEY column appears in SELECT and WHERE IS NOT NULL"],"tags":["cql","materialized-view","primary-key","unknown-column"],"backgroundTag":"resource-not-found","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"}