{"record":{"id":"6a0be2cb3370a5a7","repo":"apache/cassandra","slug":"keyspace-name-s-doesn-t-match-table-name-s","errorCode":null,"errorMessage":"Keyspace name '%s' doesn't match table name '%s'","messagePattern":"Keyspace name '(.+?)' doesn't match table name '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateIndexStatement.java","lineNumber":383,"sourceCode":"                   List<IndexTarget.Raw> rawIndexTargets,\n                   IndexAttributes attrs,\n                   boolean ifNotExists)\n        {\n            this.tableName = tableName;\n            this.indexName = indexName;\n            this.rawIndexTargets = rawIndexTargets;\n            this.attrs = attrs;\n            this.ifNotExists = ifNotExists;\n        }\n\n        public CreateIndexStatement prepare(ClientState state)\n        {\n            String keyspaceName = tableName.hasKeyspace()\n                                ? tableName.getKeyspace()\n                                : indexName.hasKeyspace() ? indexName.getKeyspace() : state.getKeyspace();\n\n            if (tableName.hasKeyspace() && !keyspaceName.equals(tableName.getKeyspace()))\n                throw ire(KEYSPACE_DOES_NOT_MATCH_TABLE, keyspaceName, tableName);\n\n            if (indexName.hasKeyspace() && !keyspaceName.equals(indexName.getKeyspace()))\n                throw ire(KEYSPACE_DOES_NOT_MATCH_INDEX, keyspaceName, tableName);\n            \n            // Set the configured default 2i implementation if one isn't specified with USING:\n            if (attrs.customClass == null)\n            {\n                if (DatabaseDescriptor.getDefaultSecondaryIndexEnabled())\n                    attrs.customClass = DatabaseDescriptor.getDefaultSecondaryIndex();\n                else\n                    // However, operators may require an implementation be specified\n                    throw ire(MUST_SPECIFY_INDEX_IMPLEMENTATION);\n            }\n            \n            // If we explicitly specify the index type \"legacy_local_table\", we can just clear the custom class, and the\n            // non-custom 2i creation process will begin. Otherwise, if an index type has been specified with \n            // USING, make sure the appropriate custom index is created.\n            if (attrs.customClass != null)","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateIndexStatement.java#L365-L401","documentation":"CREATE INDEX validates that the keyspace explicitly given in the table name matches the resolved keyspace (from table name, index name, or session keyspace). A fully-qualified table name whose keyspace disagrees with the resolved one is rejected as ambiguous/inconsistent.","triggerScenarios":"Run e.g. `CREATE INDEX ON otherks.t (col)` while the statement resolves a different keyspaceName — specifically when tableName.hasKeyspace() and that keyspace differs from the computed keyspaceName (which can come from the index name or the session's USE keyspace).","commonSituations":"Copy-pasting a statement from one environment/namespace to another; scripts with a hardcoded keyspace in the table name executed against a session connected to a different keyspace; confusion between keyspace qualifiers on the table vs index name.","solutions":["Remove the keyspace qualifier from the table name, or make it match the session keyspace.","Fully qualify BOTH the table and the index with the same keyspace: CREATE INDEX ks.idx ON ks.t(col).","Fix the cqlsh/script session: USE <correct_keyspace>; before running the statement."],"exampleFix":"// before (session is on ks2)\nCREATE INDEX ON ks1.t (col);\n\n// after\nUSE ks1;\nCREATE INDEX ON t (col);","handlingStrategy":"validation","validationCode":"if (tableName.includesKeyspace() && session.getLoggedKeyspace() != null && !tableName.getKeyspace().equals(session.getLoggedKeyspace())) throw new IllegalArgumentException(\"Table keyspace disagrees with session keyspace\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (InvalidQueryException e) { if (e.getMessage().startsWith(\"Keyspace name\")) { /* strip keyspace qualifiers or align with USE */ } else throw e; }","preventionTips":["Always fully qualify both table and index with the same keyspace","Avoid mixing USE-keyspace sessions with qualified identifiers in scripts","Templatize keyspaces with a single variable in generated DDL"],"tags":["cassandra","cql","keyspace","naming"],"backgroundTag":"invalid-argument-value","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"}