{"record":{"id":"5ef91bb68f7f133e","repo":"prestodb/presto","slug":"constraint-not-found","errorCode":null,"errorMessage":"Constraint not found","messagePattern":"Constraint not found","errorType":"exception","errorClass":"TableConstraintNotFoundException","httpStatus":null,"severity":"error","filePath":"presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java","lineNumber":1665,"sourceCode":"    public MetastoreOperationResult dropConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, String constraintName)\n    {\n        Optional<org.apache.hadoop.hive.metastore.api.Table> source = getTable(metastoreContext, databaseName, tableName);\n        if (!source.isPresent()) {\n            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));\n        }\n\n        try {\n            retry()\n                    .stopOnIllegalExceptions()\n                    .run(\"dropConstraint\", stats.getDropConstraint().wrap(() ->\n                            getMetastoreClientThenCall(metastoreContext, client -> {\n                                client.dropConstraint(databaseName, tableName, constraintName);\n                                return null;\n                            })));\n            return EMPTY_RESULT;\n        }\n        catch (NoSuchObjectException e) {\n            throw new TableConstraintNotFoundException(Optional.of(constraintName));\n        }\n        catch (TException e) {\n            throw new PrestoException(HIVE_METASTORE_ERROR, e);\n        }\n        catch (Exception e) {\n            throw propagate(e);\n        }\n    }\n\n    @Override\n    public MetastoreOperationResult addConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, TableConstraint<String> tableConstraint)\n    {\n        Optional<org.apache.hadoop.hive.metastore.api.Table> source = getTable(metastoreContext, databaseName, tableName);\n        if (!source.isPresent()) {\n            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));\n        }\n\n        org.apache.hadoop.hive.metastore.api.Table table = source.get();","sourceCodeStart":1647,"sourceCodeEnd":1683,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java#L1647-L1683","documentation":"When the metastore's dropConstraint RPC raises NoSuchObjectException, ThriftHiveMetastore translates it into TableConstraintNotFoundException ('Constraint not found'), meaning the named constraint does not exist on the table. This is the metastore confirming the constraint name is unknown.","triggerScenarios":"Calling dropConstraint with a constraintName that was never added, was already dropped, or whose name differs from the metastore's stored constraint name (e.g. auto-generated primary/foreign key names).","commonSituations":"Idempotent migration scripts running the drop twice, dropping a constraint created under a different naming convention, case mismatch in the constraint name, constraint exists in another metastore.","solutions":["List existing table constraints (SHOW CREATE TABLE or metastore metadata) and use the exact stored constraint name.","Treat TableConstraintNotFoundException as success in idempotent cleanup scripts and skip/retry the drop.","Verify against the correct metastore environment.","If the constraint name was auto-generated at ADD time, look it up from the table metadata before dropping."],"exampleFix":"// before\nmetastore.dropConstraint(context, db, table, \"my_fk\");\n\n// after: idempotent drop\ntry {\n    metastore.dropConstraint(context, db, table, constraintName);\n} catch (TableConstraintNotFoundException e) {\n    // already gone; ignore\n}","handlingStrategy":"try-catch","validationCode":"boolean exists = metastore.getTable(metastoreContext, db, table).isPresent();\n// list constraints from table metadata and confirm constraintName matches exactly before dropping","typeGuard":null,"tryCatchPattern":"try {\n    metastore.dropConstraint(context, db, table, constraintName);\n} catch (TableConstraintNotFoundException e) {\n    // treat as idempotent success\n}","preventionTips":["Read the exact constraint name from table metadata before dropping.","Write idempotent migration scripts.","Don't assume constraint naming conventions; query actual names."],"tags":["hive","metastore","constraint","ddl"],"backgroundTag":"constraint-not-found","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}