{"record":{"id":"a9aa99de6db8c04e","repo":"apache/cassandra","slug":"cannot-use-create-table-like-on-a-materialized-vie","errorCode":null,"errorMessage":"Cannot use CREATE TABLE LIKE on a materialized view '%s.%s'.","messagePattern":"Cannot use CREATE TABLE LIKE on a materialized view '(.+?)\\.(.+?)'\\.","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java","lineNumber":139,"sourceCode":"    @Override\n    public Keyspaces apply(ClusterMetadata metadata)\n    {\n        Keyspaces schema = metadata.schema.getKeyspaces();\n        KeyspaceMetadata sourceKeyspaceMeta = schema.getNullable(sourceKeyspace);\n\n        if (null == sourceKeyspaceMeta)\n            throw ire(\"Source Keyspace '%s' doesn't exist\", sourceKeyspace);\n\n        TableMetadata sourceTableMeta = sourceKeyspaceMeta.getTableOrViewNullable(sourceTableName);\n\n        if (null == sourceTableMeta)\n            throw ire(\"Source Table '%s.%s' doesn't exist\", sourceKeyspace, sourceTableName);\n\n        if (sourceTableMeta.isIndex())\n            throw ire(\"Cannot use CREATE TABLE LIKE on an index table '%s.%s'.\", sourceKeyspace, sourceTableName);\n\n        if (sourceTableMeta.isView())\n            throw ire(\"Cannot use CREATE TABLE LIKE on a materialized view '%s.%s'.\", sourceKeyspace, sourceTableName);\n\n        KeyspaceMetadata targetKeyspaceMeta = schema.getNullable(targetKeyspace);\n        if (null == targetKeyspaceMeta)\n            throw ire(\"Target Keyspace '%s' doesn't exist\", targetKeyspace);\n\n        if (targetKeyspaceMeta.hasTable(targetTableName))\n        {\n            if (ifNotExists)\n                return schema;\n\n            throw new AlreadyExistsException(targetKeyspace, targetTableName);\n        }\n\n        if (!sourceKeyspace.equalsIgnoreCase(targetKeyspace))\n        {\n            Set<String> missingUserTypes = Sets.newHashSet();\n            // for different keyspace, if source table used some udts and the target table also need them\n            for (ByteBuffer sourceUserTypeName : sourceTableMeta.getReferencedUserTypes())","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java#L121-L157","documentation":"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.","triggerScenarios":"`CREATE TABLE t2 LIKE <ks>.<view_name>` where the named source is a materialized view rather than a base table.","commonSituations":"Bulk schema-cloning scripts iterating getTableOrViewNullable-style listings that include views; confusing a view with its base table when copying schema.","solutions":["Use the view's base table as the LIKE source, then recreate the view on the new table with CREATE MATERIALIZED VIEW","Exclude views when enumerating candidate sources programmatically","Manually author the target table definition if the goal is a standalone table resembling the view's shape"],"exampleFix":"// before\nCREATE TABLE ks2.v_copy LIKE ks.users_by_email; -- a materialized view\n\n// after\nCREATE TABLE ks2.users_copy LIKE ks.users;\nCREATE MATERIALIZED VIEW ks2.users_by_email AS SELECT ... FROM ks2.users_copy ...;","handlingStrategy":"validation","validationCode":"// Ensure the source is a base table, not a view\nconst views = await session.execute(\n  \"SELECT view_name FROM system_schema.views WHERE keyspace_name=? AND view_name=?\",\n  [srcKs, srcName]);\nif (views.rows.length > 0) throw new Error(`${srcKs}.${srcName} is a materialized view; use its base table`);","typeGuard":"null","tryCatchPattern":"try {\n  session.execute(`CREATE TABLE t2 LIKE ${srcKs}.${srcName}`);\n} catch (e) {\n  if (/CREATE TABLE LIKE on a materialized view/.test(e.message)) {\n    // switch to the base table and recreate the view afterward\n  } else throw e;\n}","preventionTips":["Check system_schema.views when enumerating copy sources","Copy the base table and re-issue CREATE MATERIALIZED VIEW on the new table","Keep view definitions in migration scripts rather than deriving them from LIKE"],"tags":["cassandra","cql","create-table-like","materialized-view","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}