{"record":{"id":"399e6d139182459f","repo":"apache/cassandra","slug":"type-s-already-exists-in-s","errorCode":null,"errorMessage":"Type %s already exists in %s","messagePattern":"Type (.+?) already exists in (.+?)","errorType":"validation","errorClass":"ConfigurationException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/schema/SchemaTransformations.java","lineNumber":139,"sourceCode":"            {\n                Keyspaces schema = metadata.schema.getKeyspaces();\n                if (toAdd.isEmpty())\n                    return schema;\n\n                String keyspaceName = toAdd.iterator().next().keyspace;\n                KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n                if (null == keyspace)\n                    throw invalidRequest(\"Keyspace '%s' doesn't exist\", keyspaceName);\n\n                Types types = keyspace.types;\n                for (UserType type : toAdd)\n                {\n                    if (types.containsType(type.name))\n                    {\n                        if (ignoreIfExists)\n                            continue;\n\n                        throw new ConfigurationException(\"Type \" + type + \" already exists in \" + keyspaceName);\n                    }\n\n                    types = types.with(type);\n                }\n                return schema.withAddedOrReplaced(keyspace.withSwapped(types));\n            }\n\n            @Override\n            public boolean compatibleWith(ClusterMetadata metadata)\n            {\n                return metadata.directory.commonSerializationVersion.isAtLeast(Version.V0);\n            }\n        };\n    }\n\n    /**\n     * We have a set of non-local, distributed system keyspaces, e.g. system_traces, system_auth, etc.\n     * (see {@link SchemaConstants#REPLICATED_SYSTEM_KEYSPACE_NAMES}), that need to be created on cluster initialisation,","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/schema/SchemaTransformations.java#L121-L157","documentation":"Thrown as a ConfigurationException by a CreateTypes schema transformation when a UDT with the same name already exists in the keyspace and ignoreIfExists is false. Ensures duplicate user-defined type declarations fail loudly rather than being silently replaced.","triggerScenarios":"Applying SchemaTransformations.createTypes(keyspaceName, types, ignoreIfExists=false) when Types.containsType(type.name) is true — e.g. CREATE TYPE without IF NOT EXISTS for an existing UDT.","commonSituations":"Replaying DDL scripts that declare CREATE TYPE without IF NOT EXISTS; migration tools re-registering types; multi-tenant setup scripts run repeatedly.","solutions":["Use CREATE TYPE IF NOT EXISTS so existing types are skipped (ignoreIfExists=true)","Query system_schema.types for the keyspace to confirm the type name is free","Catch ConfigurationException and skip if the type definition matches expectations","Rename the new UDT if it is intended to be a distinct type"],"exampleFix":"// before\nCREATE TYPE my_ks.address (street text, city text);\n// after\nCREATE TYPE IF NOT EXISTS my_ks.address (street text, city text);","handlingStrategy":"try-catch","validationCode":"Types existing = Schema.instance.getKeyspaceMetadata(ksName).types; boolean has = existing.containsType(typeName);","typeGuard":"boolean typeExists(String ks, String t) { KeyspaceMetadata k = Schema.instance.getNullableKeyspaceMetadata(ks); return k != null && k.types.containsType(t); }","tryCatchPattern":"try { schemaChange(createTypeCql); } catch (ConfigurationException e) { if (!e.getMessage().contains(\"already exists\")) throw e; }","preventionTips":["Use CREATE TYPE IF NOT EXISTS","Declare types in one shared bootstrap script applied once","Check system_schema.types before creating types","Order type creation: dependencies before dependents"],"tags":["cassandra","udt","duplicate"],"backgroundTag":"file-already-exists","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}