{"record":{"id":"1a619ebc5b48d821","repo":"apache/cassandra","slug":"must-provide-at-least-one-partition-key-column-for","errorCode":null,"errorMessage":"Must provide at least one partition key column for materialized view '%s'","messagePattern":"Must provide at least one partition key column for materialized view '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":238,"sourceCode":"            // will throw IRE if the column doesn't exist in the base table\n            Selectable.RawIdentifier rawIdentifier = (Selectable.RawIdentifier) selector.selectable;\n            ColumnMetadata column = rawIdentifier.columnMetadata(table);\n\n            selectedColumns.add(column.name);\n        });\n\n        selectedColumns.stream()\n                       .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);","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L220-L256","documentation":"A materialized view must define its own primary key, which requires at least one partition key column. An empty PARTITION KEY / PRIMARY KEY clause leaves the view without a partitioner, so creation is rejected.","triggerScenarios":"CREATE MATERIALIZED VIEW ... PRIMARY KEY (() , ck) or omitted partition key columns such that partitionKeyColumns is empty at validation.","commonSituations":"Syntax mistakes in the PRIMARY KEY clause (parenthesization leaving the partition key empty); assuming the base partition key is inherited automatically.","solutions":["Explicitly include at least one column in the view's PARTITION KEY, e.g. PRIMARY KEY (pk, ck)","Ensure the listed partition key columns also appear in the SELECT list (they are usually required in the WHERE IS NOT NULL clauses too)"],"exampleFix":"// before\nCREATE MATERIALIZED VIEW mv AS SELECT ck, v FROM base WHERE ck IS NOT NULL PRIMARY KEY (( ) , ck);\n// after\nCREATE MATERIALIZED VIEW mv AS SELECT pk, ck, v FROM base WHERE pk IS NOT NULL AND ck IS NOT NULL PRIMARY KEY (pk, ck);","handlingStrategy":"validation","validationCode":"if (partitionKeyCols.isEmpty()) throw new IllegalArgumentException(\"MV needs at least one partition key column\");","typeGuard":null,"tryCatchPattern":"try { session.execute(createMvStmt); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"at least one partition key\")) { /* fix PRIMARY KEY clause */ } }","preventionTips":["Always write PRIMARY KEY with a non-empty partition-key section, e.g. ((pk), ck)","Validate generated DDL with a parser before execution"],"tags":["cql","materialized-view","partition-key"],"backgroundTag":"missing-required-argument","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"}