Metacenta

BI semantic layer · rule semantic-primary-entity-unasserted

dbt semantic model keys with no unique test

A Metacenta review checks this under the rule Semantic model keys are tested unique. Everything below applies whether or not you ever commission one.

What this rule checks

For each dbt semantic model, this rule takes every primary or unique entity that names a plain column. It passes when a uniqueness test on the model covers that column alone. A natural entity is not judged, because it repeats by design.

Why it matters

MetricFlow assumes one row per key when it joins through the entity. A duplicate key multiplies every metric joined through it, and nothing fails. The number is simply wrong.

How to fix it

Test every semantic model key for uniqueness. Add a unique test on each flagged key column, in the model the semantic model reads. A where clause on the test stops it counting, so test every row.

Before:

semantic_models:
  - name: customers
    model: ref('dim_customers')
    entities:
      - name: customer
        type: primary
        expr: customer_id

After:

models:
  - name: dim_customers
    columns:
      - name: customer_id
        data_tests:
          - unique
          - not_null

When it is fine to leave

If the model really can hold two rows per value, the entity is not a primary key. Declare it as natural or foreign instead. A composite grain test does not make any one of its columns unique, so it does not pass here.

What we need to check it

manifest.json alone. unique, the dbt_expectations uniqueness tests and a one-column unique_combination_of_columns count. A test limited by where does not. An entity built from an expression cannot carry a column test, so it is not judged.