prestodb/presto · error · TableConstraintNotFoundException

Constraint not found

Error message

Constraint not found

What it means

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.

Source

Thrown at presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java:1665

    public MetastoreOperationResult dropConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, String constraintName)
    {
        Optional<org.apache.hadoop.hive.metastore.api.Table> source = getTable(metastoreContext, databaseName, tableName);
        if (!source.isPresent()) {
            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
        }

        try {
            retry()
                    .stopOnIllegalExceptions()
                    .run("dropConstraint", stats.getDropConstraint().wrap(() ->
                            getMetastoreClientThenCall(metastoreContext, client -> {
                                client.dropConstraint(databaseName, tableName, constraintName);
                                return null;
                            })));
            return EMPTY_RESULT;
        }
        catch (NoSuchObjectException e) {
            throw new TableConstraintNotFoundException(Optional.of(constraintName));
        }
        catch (TException e) {
            throw new PrestoException(HIVE_METASTORE_ERROR, e);
        }
        catch (Exception e) {
            throw propagate(e);
        }
    }

    @Override
    public MetastoreOperationResult addConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, TableConstraint<String> tableConstraint)
    {
        Optional<org.apache.hadoop.hive.metastore.api.Table> source = getTable(metastoreContext, databaseName, tableName);
        if (!source.isPresent()) {
            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
        }

        org.apache.hadoop.hive.metastore.api.Table table = source.get();

View on GitHub (pinned to 55bb57d202)

Solutions

  1. List existing table constraints (SHOW CREATE TABLE or metastore metadata) and use the exact stored constraint name.
  2. Treat TableConstraintNotFoundException as success in idempotent cleanup scripts and skip/retry the drop.
  3. Verify against the correct metastore environment.
  4. If the constraint name was auto-generated at ADD time, look it up from the table metadata before dropping.

Example fix

// before
metastore.dropConstraint(context, db, table, "my_fk");

// after: idempotent drop
try {
    metastore.dropConstraint(context, db, table, constraintName);
} catch (TableConstraintNotFoundException e) {
    // already gone; ignore
}
Defensive patterns

Strategy: try-catch

Validate before calling

boolean exists = metastore.getTable(metastoreContext, db, table).isPresent();
// list constraints from table metadata and confirm constraintName matches exactly before dropping

Try / catch

try {
    metastore.dropConstraint(context, db, table, constraintName);
} catch (TableConstraintNotFoundException e) {
    // treat as idempotent success
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/5ef91bb68f7f133e. Report an issue: GitHub.