{"record":{"id":"c7947fffe8cf7f64","repo":"apache/cassandra","slug":"statement-on-keyspace-s-cannot-refer-to-a-user-ty","errorCode":null,"errorMessage":"Statement on keyspace %s cannot refer to a user type in keyspace %s; user types can only be used in the keyspace they are defined in","messagePattern":"Statement on keyspace (.+?) cannot refer to a user type in keyspace (.+?); user types can only be used in the keyspace they are defined in","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/CQL3Type.java","lineNumber":1013,"sourceCode":"            public RawUT freeze()\n            {\n                return new RawUT(name, true);\n            }\n\n            @Override\n            public void validate(ClientState state, String name)\n            {\n                // nothing to do here\n            }\n\n            public CQL3Type prepare(String keyspace, Types udts) throws InvalidRequestException\n            {\n                if (name.hasKeyspace())\n                {\n                    // The provided keyspace is the one of the current statement this is part of. If it's different from the keyspace of\n                    // the UTName, we reject since we want to limit user types to their own keyspace (see #6643)\n                    if (!keyspace.equals(name.getKeyspace()))\n                        throw new InvalidRequestException(String.format(\"Statement on keyspace %s cannot refer to a user type in keyspace %s; \"\n                                                                        + \"user types can only be used in the keyspace they are defined in\",\n                                                                        keyspace, name.getKeyspace()));\n                }\n                else\n                {\n                    name.setKeyspace(keyspace);\n                }\n\n                UserType type = udts.getNullable(name.getUserTypeName());\n                if (type == null)\n                    throw new InvalidRequestException(\"Unknown type \" + name);\n\n                if (frozen)\n                    type = type.freeze();\n                return new UserDefined(name.toString(), type);\n            }\n\n            public boolean referencesUserType(String name)","sourceCodeStart":995,"sourceCodeEnd":1031,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/CQL3Type.java#L995-L1031","documentation":"A statement executed in one keyspace referenced a user-defined type qualified with a different keyspace. Cassandra restricts user types to the keyspace where they are defined (CASSANDRA-6643); cross-keyspace type references are rejected.","triggerScenarios":"Running `CREATE TABLE ks1.t (x ks2.mytype ...)` or any DDL/DML in ks1 that names a type as `ks2.mytype` where ks2 != the statement's keyspace.","commonSituations":"Copy-pasting schema DDL between keyspaces; trying to share a UDT across keyspaces; scripts that set one USE keyspace but fully qualify types from another.","solutions":["Recreate the UDT in the statement's keyspace and reference it unqualified","Run the statement in the keyspace where the type is defined (USE ks2; or fully qualify the table too)","Deduplicate the type definition per keyspace in migration scripts"],"exampleFix":"// before (in ks1)\nCREATE TABLE ks1.t (id uuid PRIMARY KEY, a ks2.mytype);\n// after\n// in ks1:\nCREATE TYPE ks1.mytype (field text);\nCREATE TABLE ks1.t (id uuid PRIMARY KEY, a frozen<mytype>);","handlingStrategy":"validation","validationCode":"// before referencing a UDT, ensure its keyspace equals the statement's keyspace\nvoid checkUdtKeyspace(String statementKeyspace, String udtQualified) {\n    int dot = udtQualified.indexOf('.');\n    if (dot > 0 && !udtQualified.substring(0, dot).equalsIgnoreCase(statementKeyspace))\n        throw new IllegalArgumentException(\"UDT \" + udtQualified + \" must live in keyspace \" + statementKeyspace);\n}","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"cannot refer to a user type in keyspace\")) { /* recreate type in target keyspace */ } else throw e; }","preventionTips":["Keep UDT definitions duplicated per keyspace where needed","Never fully-qualify a UDT from another keyspace in DDL","Standardize migrations per keyspace to avoid cross-keyspace references"],"tags":["cql","schema","udt","keyspace"],"backgroundTag":"schema-validation-failed","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"}