{"record":{"id":"4e7e363c9106fe28","repo":"apache/iceberg","slug":"invalid-primary-key-s-column-s-does-not-exi-4e7e36","errorCode":null,"errorMessage":"Invalid primary key '%s'. Column '%s' does not exist.","messagePattern":"Invalid primary key '(.+?)'\\. Column '(.+?)' does not exist\\.","errorType":"validation","errorClass":"ValidationException","httpStatus":null,"severity":"error","filePath":"flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/FlinkSchemaUtil.java","lineNumber":358,"sourceCode":"    final Map<String, Column> columnsByNameLookup =\n        columns.stream().collect(Collectors.toMap(Column::getName, Function.identity()));\n\n    final Set<String> duplicateColumns =\n        primaryKey.getColumns().stream()\n            .filter(name -> Collections.frequency(primaryKey.getColumns(), name) > 1)\n            .collect(Collectors.toSet());\n\n    if (!duplicateColumns.isEmpty()) {\n      throw new ValidationException(\n          String.format(\n              \"Invalid primary key '%s'. A primary key must not contain duplicate columns. Found: %s\",\n              primaryKey.getName(), duplicateColumns));\n    }\n\n    for (String columnName : primaryKey.getColumns()) {\n      Column column = columnsByNameLookup.get(columnName);\n      if (column == null) {\n        throw new ValidationException(\n            String.format(\n                \"Invalid primary key '%s'. Column '%s' does not exist.\",\n                primaryKey.getName(), columnName));\n      }\n\n      if (!column.isPhysical()) {\n        throw new ValidationException(\n            String.format(\n                \"Invalid primary key '%s'. Column '%s' is not a physical column.\",\n                primaryKey.getName(), columnName));\n      }\n\n      final LogicalType columnType = column.getDataType().getLogicalType();\n      if (columnType.isNullable()) {\n        throw new ValidationException(\n            String.format(\n                \"Invalid primary key '%s'. Column '%s' is nullable.\",\n                primaryKey.getName(), columnName));","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/FlinkSchemaUtil.java#L340-L376","documentation":"FlinkSchemaUtil.validatePrimaryKey throws this ValidationException while converting a Flink ResolvedSchema into an Iceberg schema when a primary-key constraint references a column name that is not present in the schema's columns. The conversion cannot build an Iceberg identifier field for a nonexistent column, so it fails fast. It is raised from toResolvedSchema during table schema conversion.","triggerScenarios":"Calling FlinkSchemaUtil.toResolvedSchema (or creating an Iceberg-backed Flink table via TableEnvironment) where the DDL's PRIMARY KEY (col) clause names a column that does not exist in the column list — typically a typo or a column defined only in a computed/virtual expression key.","commonSituations":"Typos in PRIMARY KEY clauses after renaming a column; copying DDL between tables; defining a primary key over a metadata or computed column that exists in the Flink catalog but not as a physical schema column.","solutions":["Fix the PRIMARY KEY clause to reference an existing column name exactly as declared.","Check the schema column names with `table.getResolvedSchema().getColumnNames()` or DESCRIBE before declaring the key.","If the key column is computed, materialize it as a physical column instead."],"exampleFix":"// before\nCREATE TABLE iceberg_table (\n  id BIGINT,\n  data STRING,\n  PRIMARY KEY (uid) NOT ENFORCED\n) WITH (...);\n// after\nCREATE TABLE iceberg_table (\n  id BIGINT,\n  data STRING,\n  PRIMARY KEY (id) NOT ENFORCED\n) WITH (...);","handlingStrategy":"validation","validationCode":"ResolvedSchema rs = table.getResolvedSchema();\nSet<String> cols = rs.getColumnNames().stream().collect(Collectors.toSet());\nif (!cols.containsAll(pkColumns)) {\n  throw new IllegalArgumentException(\"PK columns missing from schema: \" + pkColumns);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify key column names against DESCRIBE / getColumnNames() before declaring PRIMARY KEY.","Keep DDL and key definitions in the same migration scripts to avoid rename drift."],"tags":["flink","primary-key","schema-validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}