{"record":{"id":"7974ea0fb9ff2fec","repo":"apache/cassandra","slug":"unknown-column-s-referenced-in-primary-key-for","errorCode":null,"errorMessage":"Unknown column '%s' referenced in PRIMARY KEY for table '%s'","messagePattern":"Unknown column '(.+?)' referenced in PRIMARY KEY for table '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":296,"sourceCode":"            {\n                ((UserType) type).fieldTypes().forEach(field ->\n                {\n                    if (field.isMultiCell())\n                        throw ire(\"Non-frozen UDTs with nested non-frozen collections are not supported\");\n                });\n            }\n        });\n\n        /*\n         * Deal with PRIMARY KEY columns\n         */\n\n        HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();\n        concat(partitionKeyColumns, clusteringColumns).forEach(column ->\n        {\n            ColumnProperties properties = columns.get(column);\n            if (null == properties)\n                throw ire(\"Unknown column '%s' referenced in PRIMARY KEY for table '%s'\", column, tableName);\n\n            if (!primaryKeyColumns.add(column))\n                throw ire(\"Duplicate column '%s' in PRIMARY KEY clause for table '%s'\", column, tableName);\n\n            AbstractType<?> type = properties.type;\n            if (type.isMultiCell())\n            {\n                CQL3Type cqlType = properties.cqlType;\n                if (type.isCollection())\n                    throw ire(\"Invalid non-frozen collection type %s for PRIMARY KEY column '%s'\", cqlType, column);\n                else\n                    throw ire(\"Invalid non-frozen user-defined type %s for PRIMARY KEY column '%s'\", cqlType, column);\n            }\n\n            if (type.isCounter())\n                throw ire(\"counter type is not supported for PRIMARY KEY column '%s'\", column);\n\n            if (type.referencesDuration())","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L278-L314","documentation":"CreateTableStatement.builder PRIMARY KEY handling: a column listed in the PRIMARY KEY clause was not declared in the column definitions, so the table cannot be built; the message names the unknown column and table.","triggerScenarios":"`CREATE TABLE t (a int PRIMARY KEY (b), c int)` — PRIMARY KEY references column 'b' that is not defined.","commonSituations":"Typos in column names inside PRIMARY KEY (...); refactoring column names without updating the PRIMARY KEY clause; generated DDL referencing missing columns.","solutions":["Add the missing column to the column definitions, e.g. `b text`","Fix the typo so the PRIMARY KEY references a declared column"],"exampleFix":"// before\nCREATE TABLE t (a int, PRIMARY KEY (b));\n// after\nCREATE TABLE t (a int, b text, PRIMARY KEY (b));","handlingStrategy":"validation","validationCode":"Set<String> declared = columnNames; Set<String> referenced = primaryKeyColumns; if (!declared.containsAll(referenced)) throw new IllegalArgumentException(\"PRIMARY KEY references undeclared columns: \" + new HashSet<>(referenced - declared));","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"referenced in PRIMARY KEY\")) { /* parse the column name from the message, add or correct its declaration */ } else throw e; }","preventionTips":["Define all columns before writing the PRIMARY KEY clause","Keep column names and key references in a single schema constant/module","Run schema DDL through a lint/parse step before applying"],"tags":["cql","schema","primary-key"],"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"}