{"record":{"id":"1ffbf0dcf4163383","repo":"dbt-labs/dbt-core","slug":"invalid-foreign-key-column-column-name","errorCode":null,"errorMessage":"Invalid foreign key column: {{ column_name }}","messagePattern":"Invalid foreign key column: (.+?)\\}","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/constraints.sql","lineNumber":250,"sourceCode":"        {% if local_md5 %}\n          {{ exceptions.warn(\"Constraint of type \" ~ type ~ \" with no `name` provided. Generating hash instead for relation \" ~ relation.identifier) }}\n          {%- set name = local_md5(\"foreign_key;\" ~ relation.identifier ~ \";\" ~ constraint.get('expression') ~ \";\") -%}\n        {% else %}\n          {{ exceptions.raise_compiler_error(\"Constraint of type \" ~ type ~ \" with no `name` provided, and no md5 utility.\") }}\n        {% endif %}    \n      {% endif %}\n\n      {% set stmt = \"alter table \" ~ relation.render() ~ \" add constraint \" ~ name ~ \" foreign key\" ~ constraint.get('expression') %}\n    {% else %}\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 foreign key column: ' ~ column_name) }}\n        {% else %}\n          {% set quoted_name = adapter.quote(column['name']) %}\n          {% do quoted_names.append(quoted_name) %}\n        {% endif %}\n      {% endfor %}\n\n      {% set joined_names = quoted_names|join(\", \") %}\n\n      {% set parent = constraint.get('to') %}\n      {% if not parent %}\n        {{ exceptions.raise_compiler_error('No parent table defined for foreign key: ' ~ expression) }}\n      {% endif %}\n      {% if not \".\" in parent %}\n        {% set parent_relation = api.Relation.create(database=relation.database, schema=relation.schema, identifier=parent, type='table') %}\n        {% set parent = parent_relation.render() %}\n      {% endif %}\n\n      {% if not name %}","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/constraints.sql#L232-L268","documentation":"During foreign key constraint DDL generation for Databricks, each column listed in the constraint's `columns` (or single `column_name`) is looked up in the model's `columns` dict. If a referenced column name does not exist on the model, dbt emits this warning and skips that column instead of rendering it into the `foreign key` clause. It exists because referencing a non-existent column would produce invalid DDL at the warehouse.","triggerScenarios":"A model defines a `foreign_key` constraint (model-level, with `columns: [...]`, or column-level) whose column name does not match any key in the model's `columns:` YAML block, e.g. a typo or a column added to the constraint but never declared under `columns:`.","commonSituations":"Renaming a column in the model without updating the foreign key constraint's column list; hand-writing constraint YAML with wrong casing (column lookup is case-sensitive as written); declaring the FK at model level while forgetting to list the referenced column under `columns:`.","solutions":["Open the model's YAML and add the missing column under `columns:` with a matching name","Fix the typo/casing in the foreign key constraint's `columns` (or column-level `name`) so it exactly matches a declared column","Remove the stale column entry from the constraint if it is no longer part of the key","Re-run dbt and confirm the warning no longer appears and the FK DDL includes all intended columns"],"exampleFix":"# before\ncolumns:\n  - name: order_id\ncolumn_types: ...\nconstraints:\n  - type: foreign_key\n    columns: [order_idd, customer_id]\n# after\ncolumns:\n  - name: order_id\n  - name: customer_id\nconstraints:\n  - type: foreign_key\n    columns: [order_id, customer_id]","handlingStrategy":"validation","validationCode":"# in a schema.yml hook or pre-flight script: ensure every FK column is declared\nimport yaml\ndef check_fk_columns(schema):\n    for model in schema.get('models', []):\n        cols = {c['name'] for c in model.get('columns', [])}\n        for con in model.get('constraints', []):\n            if con['type'] == 'foreign_key':\n                missing = set(con.get('columns', [])) - cols\n                assert not missing, f\"{model['name']}: FK columns missing from columns: {missing}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always declare every column referenced by a constraint under `columns:` in the model YAML","Generate/validate YAML with dbt-codegen and review constraint column lists","Add a CI lint that cross-checks constraint columns against declared columns","Treat 'Invalid foreign key column' warnings in dbt logs as CI failures"],"tags":["databricks","constraints","foreign-key","jinja"],"backgroundTag":"schema-validation-failed","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}