{"record":{"id":"3e8d09f557d2fc00","repo":"apache/cassandra","slug":"cannot-create-a-materialized-view-on-a-table-in-a","errorCode":null,"errorMessage":"Cannot create a materialized view on a table in a different keyspace","messagePattern":"Cannot create a materialized view on a table in a different keyspace","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":455,"sourceCode":"\n        private final LinkedHashMap<ColumnIdentifier, Boolean> clusteringOrder = new LinkedHashMap<>();\n        public final TableAttributes attrs = new TableAttributes();\n\n        public Raw(QualifiedName tableName, QualifiedName viewName, List<RawSelector> rawColumns, WhereClause whereClause, boolean ifNotExists)\n        {\n            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,","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L437-L473","documentation":"CREATE MATERIALIZED VIEW requires the base table and the view to live in the same keyspace. Cassandra throws this during statement preparation when the view name's keyspace (explicit or from the session) differs from an explicitly keyspace-qualified base table name.","triggerScenarios":"Executing CREATE MATERIALIZED VIEW other_ks.view AS SELECT ... FROM my_ks.table (or the reverse: view keyspace qualified, table keyspace omitted in a session using a different keyspace).","commonSituations":"Copy-pasting view DDL between keyspaces; forgetting that when both names are qualified they must match; running the statement while USE'd into a different keyspace than the table's.","solutions":["Create the view in the same keyspace as the base table, or remove the keyspace qualifier from the table name so it inherits the view's keyspace","Verify which keyspace the session is in (USE my_ks) if names were unqualified","Update application schema-migration scripts so view and table keyspaces match"],"exampleFix":"// before\nCREATE MATERIALIZED VIEW other_ks.orders_mv AS SELECT * FROM sales.orders WHERE order_id IS NOT NULL PRIMARY KEY (order_id);\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 (viewKeyspace && tableKeyspace && viewKeyspace !== tableKeyspace) throw new Error('View and base table must be in the same keyspace');","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (e) { if (e instanceof InvalidQueryError && /different keyspace/.test(e.message)) { /* fix keyspace qualification and retry */ } else throw e; }","preventionTips":["Always qualify both view and table names with the same keyspace","Keep schema DDL scripts explicit about keyspaces rather than relying on session USE"],"tags":["cql","materialized-view","keyspace-mismatch","schema"],"backgroundTag":"invalid-argument-value","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"}