{"record":{"id":"37c0b2a7971e84a7","repo":"dbt-labs/dbt-core","slug":"invalid-primary-key-column-column-name","errorCode":null,"errorMessage":"Invalid primary key column: {column_name}","messagePattern":"Invalid primary 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":195,"sourceCode":"        {% 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) %}\n      {% endif %}\n    {% endfor %}\n\n    {% set joined_names = quoted_names|join(\", \") %}\n    {% set pk_expression = constraint.get('expression') %}\n\n    {% set name = constraint.get('name') %}\n    {% if not name %}\n      {% if local_md5 %}\n        {{ exceptions.warn(\"Constraint of type \" ~ type ~ \" with no `name` provided. Generating hash instead for relation \" ~ relation.identifier) }}\n        {%- set hash_input = \"primary_key;\" ~ relation.identifier ~ \";\" ~ column_names ~ \";\" -%}\n        {%- if pk_expression -%}\n          {%- set hash_input = hash_input ~ pk_expression ~ \";\" -%}\n        {%- endif -%}\n        {%- set name = local_md5(hash_input) -%}","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-loader/src/dbt_macro_assets/dbt-databricks/macros/relations/constraints.sql#L177-L213","documentation":"A warning raised by the Databricks primary_key constraint macro when a column name listed in the constraint's `columns` (or the column-level constraint's own name) does not exist in the model's `columns` dictionary. The macro looks up `model['columns'][column_name]`; a miss means the ALTER TABLE ... PRIMARY KEY statement would reference a nonexistent column, so that column is skipped and warned about. Compilation still succeeds.","triggerScenarios":"A `primary_key` constraint references a column name that is misspelled, not defined under `columns:` in the model's YAML (schema.yml), or differs in case from the declared column; the lookup `model.get('columns', {}).get(column_name)` at constraints.sql:193 returns nothing.","commonSituations":"Typo in the constraint `columns` list; declaring a column-level PK constraint in dbt_project.yml or config instead of schema.yml so the column context is missing; renaming a column without updating the constraint; relying on columns inferred only from SQL (constraints need explicit `columns:` entries in YAML for this lookup).","solutions":["Fix the column name in the constraint so it exactly matches a key under `columns:` in the model's YAML.","Add the missing column definition (name plus any properties) under the model's `columns:` in schema.yml.","Check case sensitivity: Databricks column names in YAML must match exactly as declared.","Remove the stale column from the constraint's `columns` list if the column was dropped."],"exampleFix":"// before (schema.yml)\ncolumns:\n  - name: user_id\nconstraints:\n  - type: primary_key\n    columns: [userId]\n// after\nconstraints:\n  - type: primary_key\n    columns: [user_id]","handlingStrategy":"validation","validationCode":"// pre-run schema validation (Python)\nimport yaml\ncfg = yaml.safe_load(open('models/schema.yml'))\nfor m in cfg['models']:\n    cols = {c['name'] for c in m.get('columns', [])}\n    for c in m.get('constraints', []):\n        if c['type'] == 'primary_key':\n            missing = [n for n in c.get('columns', []) if n not in cols]\n            assert not missing, f\"{m['name']}: PK columns not in columns: {missing}\"","typeGuard":"def has_column(model: dict, col: str) -> bool:\n    return col in model.get('columns', {})","tryCatchPattern":null,"preventionTips":["Keep every constrained column declared under columns: in schema.yml.","Run dbt parse / dbt compile in CI and grep logs for 'Invalid primary key column'.","Use schema validation (dbt-jsonschema / schemaspy) to catch mismatches early.","Update constraints whenever columns are renamed or removed."],"tags":["dbt","jinja","databricks","constraints","schema-yml"],"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-14T11:17:12.474Z"}