{"record":{"id":"644a8ef413fd0d5b","repo":"apache/cassandra","slug":"target-keyspace-s-doesn-t-exist","errorCode":null,"errorMessage":"Target Keyspace '%s' doesn't exist","messagePattern":"Target Keyspace '(.+?)' doesn't exist","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java","lineNumber":143,"sourceCode":"        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())\n            {\n                Optional<UserType> targetUserType = targetKeyspaceMeta.types.get(sourceUserTypeName);\n                Optional<UserType> sourceUserType = sourceKeyspaceMeta.types.get(sourceUserTypeName);\n                if (targetUserType.isPresent() && sourceUserType.isPresent())","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java#L125-L161","documentation":"CREATE TABLE LIKE requires an existing target keyspace. After validating the source, CopyTableStatement.apply() looks up the target keyspace and throws this InvalidRequestException when schema.getNullable(targetKeyspace) returns null. Unlike CREATE TABLE, CREATE TABLE LIKE does not implicitly create keyspaces.","triggerScenarios":"`CREATE TABLE <targetKs>.<t> LIKE <src>` where targetKs doesn't exist on the cluster (misspelled, dropped, or not yet created).","commonSituations":"Running copy scripts on a fresh cluster where only the source keyspace was provisioned; typos in the target keyspace; cross-datacenter replication setups where the target keyspace was never created.","solutions":["Create the target keyspace first: CREATE KEYSPACE <ks> WITH replication = ...;","Correct the target keyspace name in the statement","Verify with `DESCRIBE KEYSPACES` / system_schema.keyspaces before running the copy","Provision keyspaces via migration tooling before table-copy steps"],"exampleFix":"// before\nCREATE TABLE analytics.events LIKE prod.events;\n-- analytics doesn't exist\n\n// after\nCREATE KEYSPACE analytics WITH replication = {'class':'NetworkTopologyStrategy','dc1':3};\nCREATE TABLE analytics.events LIKE prod.events;","handlingStrategy":"validation","validationCode":"// Ensure target keyspace exists\nconst rows = await session.execute(\n  \"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", [targetKs]);\nif (rows.rows.length === 0) {\n  await session.execute(`CREATE KEYSPACE ${targetKs} WITH replication = {'class':'SimpleStrategy','replication_factor':1}`);\n}","typeGuard":"null","tryCatchPattern":"try {\n  session.execute(`CREATE TABLE ${targetKs}.t2 LIKE src.t1`);\n} catch (e) {\n  if (/Target Keyspace .* doesn't exist/.test(e.message)) {\n    // create the keyspace, then retry the copy\n  } else throw e;\n}","preventionTips":["Provision target keyspaces before table-copy steps","Use idempotent CREATE KEYSPACE ... IF NOT EXISTS in bootstrap scripts","Parameterize target keyspace names per environment","Verify with system_schema.keyspaces prior to copies"],"tags":["cassandra","cql","create-table-like","keyspace-not-found"],"backgroundTag":"resource-not-found","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"}