{"record":{"id":"15eedbc1795bb483","repo":"dbt-labs/dbt-core","slug":"not-null-constraint-on-invalid-column-column-nam","errorCode":null,"errorMessage":"not_null constraint on invalid column: {column_name}","messagePattern":"not_null constraint on invalid column: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/constraints.sql","lineNumber":180,"sourceCode":"      {% else %}\n        {{ exceptions.raise_compiler_error(\"Constraint of type \" ~ type ~ \" with no `name` provided, and no md5 utility.\") }}\n      {% endif %}\n    {% endif %}\n    {% set stmt = \"alter table \" ~ relation.render() ~ \" add constraint \" ~ name ~ \" check (\" ~ expression ~ \");\" %}\n    {% do statements.append(stmt) %}\n  {% elif type == 'not_null' %}\n    {% set column_names = constraint.get('columns', []) %}\n    {% if column and not column_names %}\n      {% set column_names = [column['name']] %}\n    {% endif %}\n    {% for column_name in column_names %}\n      {% set column = model.get('columns', {}).get(column_name) %}\n      {% if column %}\n        {% set quoted_name = adapter.quote(column['name']) %}\n        {% set stmt = \"alter table \" ~ relation.render() ~ \" change column \" ~ quoted_name ~ \" set not null \" ~ (constraint.expression or \"\") ~ \";\" %}\n        {% do statements.append(stmt) %}\n      {% else %}\n        {{ exceptions.warn('not_null constraint on invalid column: ' ~ column_name) }}\n      {% endif %}\n    {% endfor %}\n  {% elif type == 'primary_key' %}\n    {% if constraint.get('warn_unenforced') %}\n      {{ exceptions.warn(\"unenforced constraint type: \" ~ type)}}\n    {% endif %}\n    {% set column_names = constraint.get('columns', []) %}\n    {% if column and not column_names %}\n      {% set column_names = [column['name']] %}\n    {% endif %}\n    {% set quoted_names = [] %}\n    {% for column_name in column_names %}\n      {% set column = model.get('columns', {}).get(column_name) %}\n      {% if not column %}\n        {{ exceptions.warn('Invalid primary key column: ' ~ column_name) }}\n      {% else %}\n        {% set quoted_name = adapter.quote(column['name']) %}\n        {% do quoted_names.append(quoted_name) %}","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/constraints.sql#L162-L198","documentation":"A `not_null` constraint was declared for a column name that does not exist in the model's `columns` dictionary (or has no column entry), so dbt cannot build the ALTER TABLE ... SET NOT NULL statement and warns instead of applying it. The constraint is silently unenforced.","triggerScenarios":"A model-level or yml constraint referencing a `column_name` that is not present in the model's rendered `model['columns']` mapping — typically a typo, or a constraint declared at model level with no matching column-level definition.","commonSituations":"Renaming a column without updating the constraint; declaring constraints in schema.yml under a column that doesn't exist in the SELECT list; copy-pasted constraint blocks referencing stale column names.","solutions":["Fix the column name in the constraint declaration to exactly match a column in the model's columns config.","Remove the constraint if the column no longer exists.","Add the column entry under the model's `columns:` in schema.yml so the constraint resolves."],"exampleFix":"# before (schema.yml, column 'user_id' not in model)\n- type: not_null\n  columns: [usr_id]\n# after\n- type: not_null\n  columns: [user_id]","handlingStrategy":"validation","validationCode":"# Validate constraint columns exist in the model before running:\nmodel_cols = set(model.get('columns', {}).keys())\nfor c in constraints:\n    for col in c.get('columns', []):\n        if col not in model_cols:\n            raise ValueError(f\"not_null constraint references unknown column: {col}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep constraint column names in sync with column definitions when renaming columns.","Add CI checks cross-referencing schema.yml constraints against model columns.","Declare constraints at the column level in schema.yml so typos surface as schema errors."],"tags":["dbt","databricks","constraints","not-null","column"],"backgroundTag":"resource-not-found","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}