dbt-labs/dbt-core · warning

Constraint of type {type} with no `name` provided. Generatin

Error message

Constraint of type {type} with no `name` provided. Generating hash instead for relation {relation.identifier}

What it means

When adding a check constraint, dbt requires a constraint name; if the constraint has no `name`, dbt falls back to generating one from an md5 hash of the relation, column, and expression, and warns about it. If no md5 utility is available it raises a compiler error instead.

Source

Thrown at crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/constraints.sql:160

  {% endfor %}

  {{ return(statements) }}
{% endmacro %}

{% macro get_constraint_sql(relation, constraint, model, column={}) %}
  {% set statements = [] %}
  {% set type = constraint.get('type', '') %}

  {% if type == 'check' %}
    {% set expression = constraint.get('expression', '') %}
    {% if not expression %}
      {{ exceptions.raise_compiler_error('Invalid check constraint expression') }}
    {% endif %}

    {% set name = constraint.get('name') %}
    {% if not name %}
      {% if local_md5 %}
        {{ exceptions.warn("Constraint of type " ~ type ~ " with no `name` provided. Generating hash instead for relation " ~ relation.identifier) }}
        {%- set name = local_md5 (relation.identifier ~ ";" ~ column.get('name', '') ~ ";" ~ expression ~ ";") -%}
      {% else %}
        {{ exceptions.raise_compiler_error("Constraint of type " ~ type ~ " with no `name` provided, and no md5 utility.") }}
      {% endif %}
    {% endif %}
    {% set stmt = "alter table " ~ relation.render() ~ " add constraint " ~ name ~ " check (" ~ expression ~ ");" %}
    {% do statements.append(stmt) %}
  {% elif type == 'not_null' %}
    {% set column_names = constraint.get('columns', []) %}
    {% if column and not column_names %}
      {% set column_names = [column['name']] %}
    {% endif %}
    {% for column_name in column_names %}
      {% set column = model.get('columns', {}).get(column_name) %}
      {% if column %}
        {% set quoted_name = adapter.quote(column['name']) %}
        {% set stmt = "alter table " ~ relation.render() ~ " change column " ~ quoted_name ~ " set not null " ~ (constraint.expression or "") ~ ";" %}
        {% do statements.append(stmt) %}

View on GitHub (pinned to 0267ce9170)

Solutions

  1. Add an explicit `name` to the check constraint definition to get stable, readable constraint names.
  2. If a generated name is acceptable, silence the warning by naming constraints anyway (hash names are opaque and unstable across edits).

Example fix

# before
- type: check
  expression: "id > 0"
# after
- type: check
  name: chk_id_positive
  expression: "id > 0"
Defensive patterns

Strategy: validation

Validate before calling

# Validate constraints in schema.yml before dbt run:
for c in model_constraints:
    if c['type'] == 'check' and not c.get('name'):
        raise ValueError(f"check constraint on {model_name} is missing a 'name'")

Prevention

When it happens

Trigger: Declaring a check constraint (in columns.constraints or model-level constraints with `persist_constraints=true`) without a `name` key on databricks; dbt generates a hashed name and warns.

Common situations: Hand-written check constraints missing the `name` field because it is optional in dbt's constraint schema but effectively required by Databricks ALTER TABLE ... ADD CONSTRAINT syntax.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of dbt-labs/dbt-core@0267ce9170 (2026-09-07). Data as JSON: /api/errors/d413e37da203c02a. Report an issue: GitHub.