apache/cassandra · error · InvalidRequestException

Cannot create a materialized view on a table in a different…

Error message

Cannot create a materialized view on a table in a different keyspace

What it means

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.

Solutions

  1. 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
  2. Verify which keyspace the session is in (USE my_ks) if names were unqualified
  3. Update application schema-migration scripts so view and table keyspaces match

Example fix

// before
CREATE MATERIALIZED VIEW other_ks.orders_mv AS SELECT * FROM sales.orders WHERE order_id IS NOT NULL PRIMARY KEY (order_id);
// after
CREATE MATERIALIZED VIEW sales.orders_mv AS SELECT * FROM sales.orders WHERE order_id IS NOT NULL PRIMARY KEY (order_id);
Defensive patterns

Strategy: validation

Validate before calling

if (viewKeyspace && tableKeyspace && viewKeyspace !== tableKeyspace) throw new Error('View and base table must be in the same keyspace');

Try / catch

try { session.execute(ddl); } catch (e) { if (e instanceof InvalidQueryError && /different keyspace/.test(e.message)) { /* fix keyspace qualification and retry */ } else throw e; }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/3e8d09f557d2fc00. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java:455

        private final LinkedHashMap<ColumnIdentifier, Boolean> clusteringOrder = new LinkedHashMap<>();
        public final TableAttributes attrs = new TableAttributes();

        public Raw(QualifiedName tableName, QualifiedName viewName, List<RawSelector> rawColumns, WhereClause whereClause, boolean ifNotExists)
        {
            this.tableName = tableName;
            this.viewName = viewName;
            this.rawColumns = rawColumns;
            this.whereClause = whereClause;
            this.ifNotExists = ifNotExists;
        }

        public CreateViewStatement prepare(ClientState state)
        {
            String keyspaceName = viewName.hasKeyspace() ? viewName.getKeyspace() : state.getKeyspace();

            if (tableName.hasKeyspace() && !keyspaceName.equals(tableName.getKeyspace()))
                throw ire("Cannot create a materialized view on a table in a different keyspace");

            if (!bindVariables.isEmpty())
                throw ire("Bind variables are not allowed in CREATE MATERIALIZED VIEW statements");

            if (null == partitionKeyColumns)
                throw ire("No PRIMARY KEY specifed for view '%s' (exactly one required)", viewName);

            return new CreateViewStatement(keyspaceName,
                                           tableName.getName(),
                                           viewName.getName(),

                                           rawColumns,
                                           partitionKeyColumns,
                                           clusteringColumns,

                                           whereClause,

                                           clusteringOrder,

View on GitHub (pinned to 88fd0f6a0e)