apache/cassandra · error · InvalidRequestException

Cannot use CREATE TABLE LIKE on a materialized view '%s.%s'.

Error message

Cannot use CREATE TABLE LIKE on a materialized view '%s.%s'.

What it means

CREATE TABLE LIKE cannot use a materialized view as its source. CopyTableStatement.apply() checks sourceTableMeta.isView() and throws this InvalidRequestException because a view is derived from its base table and cannot be cloned as a table. Views are resolvable by getTableOrViewNullable, hence a dedicated check after existence.

Source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java:139

    @Override
    public Keyspaces apply(ClusterMetadata metadata)
    {
        Keyspaces schema = metadata.schema.getKeyspaces();
        KeyspaceMetadata sourceKeyspaceMeta = schema.getNullable(sourceKeyspace);

        if (null == sourceKeyspaceMeta)
            throw ire("Source Keyspace '%s' doesn't exist", sourceKeyspace);

        TableMetadata sourceTableMeta = sourceKeyspaceMeta.getTableOrViewNullable(sourceTableName);

        if (null == sourceTableMeta)
            throw ire("Source Table '%s.%s' doesn't exist", sourceKeyspace, sourceTableName);

        if (sourceTableMeta.isIndex())
            throw ire("Cannot use CREATE TABLE LIKE on an index table '%s.%s'.", sourceKeyspace, sourceTableName);

        if (sourceTableMeta.isView())
            throw ire("Cannot use CREATE TABLE LIKE on a materialized view '%s.%s'.", sourceKeyspace, sourceTableName);

        KeyspaceMetadata targetKeyspaceMeta = schema.getNullable(targetKeyspace);
        if (null == targetKeyspaceMeta)
            throw ire("Target Keyspace '%s' doesn't exist", targetKeyspace);

        if (targetKeyspaceMeta.hasTable(targetTableName))
        {
            if (ifNotExists)
                return schema;

            throw new AlreadyExistsException(targetKeyspace, targetTableName);
        }

        if (!sourceKeyspace.equalsIgnoreCase(targetKeyspace))
        {
            Set<String> missingUserTypes = Sets.newHashSet();
            // for different keyspace, if source table used some udts and the target table also need them
            for (ByteBuffer sourceUserTypeName : sourceTableMeta.getReferencedUserTypes())

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Use the view's base table as the LIKE source, then recreate the view on the new table with CREATE MATERIALIZED VIEW
  2. Exclude views when enumerating candidate sources programmatically
  3. Manually author the target table definition if the goal is a standalone table resembling the view's shape

Example fix

// before
CREATE TABLE ks2.v_copy LIKE ks.users_by_email; -- a materialized view

// after
CREATE TABLE ks2.users_copy LIKE ks.users;
CREATE MATERIALIZED VIEW ks2.users_by_email AS SELECT ... FROM ks2.users_copy ...;
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the source is a base table, not a view
const views = await session.execute(
  "SELECT view_name FROM system_schema.views WHERE keyspace_name=? AND view_name=?",
  [srcKs, srcName]);
if (views.rows.length > 0) throw new Error(`${srcKs}.${srcName} is a materialized view; use its base table`);

Type guard

null

Try / catch

try {
  session.execute(`CREATE TABLE t2 LIKE ${srcKs}.${srcName}`);
} catch (e) {
  if (/CREATE TABLE LIKE on a materialized view/.test(e.message)) {
    // switch to the base table and recreate the view afterward
  } else throw e;
}

Prevention

When it happens

Trigger: `CREATE TABLE t2 LIKE <ks>.<view_name>` where the named source is a materialized view rather than a base table.

Common situations: Bulk schema-cloning scripts iterating getTableOrViewNullable-style listings that include views; confusing a view with its base table when copying schema.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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