{"record":{"id":"5c548e94c69f5c31","repo":"apache/cassandra","slug":"cannot-use-create-table-like-on-an-index-table-s","errorCode":null,"errorMessage":"Cannot use CREATE TABLE LIKE on an index table '%s.%s'.","messagePattern":"Cannot use CREATE TABLE LIKE on an index table '(.+?)\\.(.+?)'\\.","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java","lineNumber":136,"sourceCode":"        return metadata.directory.commonSerializationVersion.isAtLeast(Version.V5);\n    }\n\n    @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        {","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java#L118-L154","documentation":"CREATE TABLE LIKE cannot be used with an index (specifically a legacy secondary-index-backed table) as its source. CopyTableStatement.apply() checks sourceTableMeta.isIndex() after resolving the source and throws this InvalidRequestException, because an index is derived metadata, not a standalone table that can be cloned.","triggerScenarios":"`CREATE TABLE t2 LIKE <ks>.<name>` where <name> resolves to an index table (e.g. the internal table backing a 2i / SAI index).","commonSituations":"Scripting schema clones by iterating all names in a keyspace (system_schema.tables includes index-backed entries); attempting to copy an index's structure instead of creating an index on the copied table.","solutions":["Point LIKE at the base table the index belongs to, then recreate the index on the new table with CREATE INDEX/CREATE CUSTOM INDEX","Filter out index tables when generating CREATE TABLE LIKE statements programmatically","Drop/rename the confusingly named index if it shadows a table name (rare)"],"exampleFix":"// before\nCREATE TABLE ks2.idx_copy LIKE ks.tab_by_email_idx;\n\n// after\nCREATE TABLE ks2.tab_copy LIKE ks.tab;\nCREATE INDEX ON ks2.tab_copy (email);","handlingStrategy":"validation","validationCode":"// Skip index-backed tables by joining system_schema.indexes\nconst rows = await session.execute(\n  `SELECT t.table_name FROM system_schema.tables t\n   LEFT JOIN system_schema.indexes i\n     ON t.keyspace_name=i.keyspace_name AND t.table_name=i.table_name\n   WHERE t.keyspace_name=? AND t.table_name=? AND i.index_name IS NULL`,\n  [srcKs, srcTable]);\nif (rows.rows.length === 0) throw new Error(`${srcKs}.${srcTable} is an index table; use its base table`);","typeGuard":"null","tryCatchPattern":"try {\n  session.execute(`CREATE TABLE t2 LIKE ${name}`);\n} catch (e) {\n  if (/CREATE TABLE LIKE on an index table/.test(e.message)) {\n    // resolve base table and recreate the index afterward\n  } else throw e;\n}","preventionTips":["When cloning whole keyspaces, filter out index tables from system_schema.tables","Recreate indexes with CREATE INDEX on the copied table instead of copying them","Exclude system/internal tables from bulk copy scripts"],"tags":["cassandra","cql","create-table-like","index","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"}