Metacenta

Testing · rule model-grain-not-asserted

dbt model grain: no uniqueness test

A Metacenta review checks this under the rule Tested models assert their grain (a uniqueness test). Everything below applies whether or not you ever commission one.

What this rule checks

Among models that have tests, this rule flags those where nothing asserts one row per key. unique, dbt_utils.unique_combination_of_columns and the dbt_expectations uniqueness tests count. A uniqueness test limited by where or row_condition does not, because it covers only some rows.

Why it matters

A duplicate row doubles every sum built on it, and nothing fails when it happens. It is the most common reason a metric cannot be reconciled.

How to fix it

Test that no key appears twice in any table. Add unique on the key column. For a grain defined by several columns, use dbt_utils.unique_combination_of_columns over exactly those columns.

Before:

models:
  - name: fct_order_lines
    columns:
      - name: order_id
        data_tests:
          - not_null

After:

models:
  - name: fct_order_lines
    data_tests:
      - dbt_utils.unique_combination_of_columns:
          combination_of_columns:
            - order_id
            - line_number

When it is fine to leave

Some models have no grain by design, such as an append-only event log where two identical rows are two real events. Say so in the model's description. Models with no tests at all are not judged here; the rule on untested models covers them.

What we need to check it

manifest.json alone. We name any uniqueness test that was left out because of a where filter, so you can see why it did not count.

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. dbt Labs' rule requires a not_null test as well as a uniqueness one; that half is model-no-not-null-test here, so their single rule is these two checks together.

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