Metacenta

Testing · rule model-no-not-null-test

dbt key columns: no not_null test

A Metacenta review checks this under the rule Tested models assert key columns are populated. Everything below applies whether or not you ever commission one.

What this rule checks

Among models that have tests, this rule flags those with no not_null test on any column. dbt_expectations' expect_column_values_to_not_be_null also counts. A test limited by where or row_condition does not. The manifest does not say which column is the key, so any whole-model not_null test passes.

Why it matters

A null key drops rows silently from every inner join downstream. The totals still look plausible, so nobody notices until a figure fails to reconcile.

How to fix it

Test that key columns are never empty. Add not_null to the columns that must always be populated: the grain column, and any column other models join on.

Before:

      - name: customer_id
        data_tests:
          - relationships:
              to: ref('dim_customers')
              field: customer_id

After:

      - name: customer_id
        data_tests:
          - not_null
          - relationships:
              to: ref('dim_customers')
              field: customer_id

When it is fine to leave

A key that can be null by design, such as an optional foreign key on a fact table, should not carry not_null. Test the columns that must be populated instead. This rule is low severity for that reason: the gap is real, but the right column to test is a judgement.

What we need to check it

manifest.json alone.

Published rules it corresponds to

dbt_project_evaluator, rule fct_missing_primary_key_tests. Ours checks a subset of what it flags; theirs is stricter. the other half of dbt Labs' rule, whose uniqueness requirement is model-grain-not-asserted here.

This means our check corresponds to their rule. It does not mean the publisher reviewed or endorses it.