{"record":{"id":"5dc8bc3e8eeadf82","repo":"apache/cassandra","slug":"no-primary-key-specifed-for-view-s-exactly-one","errorCode":null,"errorMessage":"No PRIMARY KEY specifed for view '%s' (exactly one required)","messagePattern":"No PRIMARY KEY specifed for view '(.+?)' \\(exactly one required\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":461,"sourceCode":"            this.tableName = tableName;\n            this.viewName = viewName;\n            this.rawColumns = rawColumns;\n            this.whereClause = whereClause;\n            this.ifNotExists = ifNotExists;\n        }\n\n        public CreateViewStatement prepare(ClientState state)\n        {\n            String keyspaceName = viewName.hasKeyspace() ? viewName.getKeyspace() : state.getKeyspace();\n\n            if (tableName.hasKeyspace() && !keyspaceName.equals(tableName.getKeyspace()))\n                throw ire(\"Cannot create a materialized view on a table in a different keyspace\");\n\n            if (!bindVariables.isEmpty())\n                throw ire(\"Bind variables are not allowed in CREATE MATERIALIZED VIEW statements\");\n\n            if (null == partitionKeyColumns)\n                throw ire(\"No PRIMARY KEY specifed for view '%s' (exactly one required)\", viewName);\n\n            return new CreateViewStatement(keyspaceName,\n                                           tableName.getName(),\n                                           viewName.getName(),\n\n                                           rawColumns,\n                                           partitionKeyColumns,\n                                           clusteringColumns,\n\n                                           whereClause,\n\n                                           clusteringOrder,\n                                           attrs,\n\n                                           ifNotExists);\n        }\n\n        public void setPartitionKeyColumns(List<ColumnIdentifier> columns)","sourceCodeStart":443,"sourceCodeEnd":479,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L443-L479","documentation":"A materialized view must define exactly one PRIMARY KEY specification. Cassandra throws this when the view's PRIMARY KEY clause yields no partition key columns, which happens when the PRIMARY KEY clause is missing or malformed during prepare().","triggerScenarios":"CREATE MATERIALIZED VIEW without a PRIMARY KEY clause, or a parenthesized key spec that the grammar did not map to partition key columns.","commonSituations":"Omitting 'PRIMARY KEY (...)' because normal tables can omit it; malformed nesting of the key tuple; tooling that strips the clause.","solutions":["Add an explicit PRIMARY KEY clause listing the view's partition key column(s) first, then clustering columns","Ensure every partition key column is also selected and declared IS NOT NULL in the WHERE clause"],"exampleFix":"// before\nCREATE MATERIALIZED VIEW sales.orders_mv AS SELECT * FROM sales.orders WHERE order_id IS NOT NULL;\n// after\nCREATE MATERIALIZED VIEW sales.orders_mv AS SELECT * FROM sales.orders WHERE order_id IS NOT NULL PRIMARY KEY (order_id);","handlingStrategy":"validation","validationCode":"if (!/PRIMARY\\s+KEY\\s*\\(/i.test(ddl)) throw new Error('CREATE MATERIALIZED VIEW requires a PRIMARY KEY clause');","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (e) { if (e instanceof InvalidQueryError && /No PRIMARY KEY/.test(e.message)) { /* append PRIMARY KEY clause */ } else throw e; }","preventionTips":["Always include PRIMARY KEY (...) in materialized view DDL templates","Verify partition key columns are selected and constrained with IS NOT NULL"],"tags":["cql","materialized-view","primary-key","schema"],"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"}