> ## Documentation Index
> Fetch the complete documentation index at: https://docs.omni.co/llms.txt
> Use this file to discover all available pages before exploring further.

# primary_key

> Sets the primary key on a view for preventing fan outs in calculations.

<Note>
  By default, Omni will assign fields labeled like `id` or `table_id` as primary keys when generating a model. For example, the `users_id` on a `users` table. Primary keys can also be automatically set using dbt constraints.
</Note>

Primary keys are used to prevent fan outs when calculating metrics with more than one table, in conjunction with the join relationship.

They can be defined using this parameter or in the workbook by hovering over a field and clicking <Icon icon="ellipsis-vertical" iconType="solid" /> **> Modeling > Primary key**.

For situations where a database column is not available for `primary_key` declaration - for example, compound keys like `CONCAT(id, '-', user_id)` - Omni recommends creating a new field and setting that field as the primary key. Alternatively, for compound primary keys, you can set [`custom_compound_primary_key_sql`](/modeling/views/parameters/custom-compound-primary-key-sql) at the view level to define an array of fields that make up the view's primary key.

## Syntax

```yaml theme={null}
<dimension_name>:
  primary_key: true
```

## Properties

<ParamField path="dimension_name" type="object">
  The name of the dimension. Dimension names must:

  * Be unique within the view
  * Start with a letter
  * Contain only alphanumeric characters and underscores

  <Expandable title="dimension_name properties" defaultOpen="true">
    <ParamField path="primary_key" type="boolean">
      When set to `true`, designates this field as the primary key for the view.
    </ParamField>
  </Expandable>
</ParamField>

## Examples

```yaml title="Simple primary key" theme={null}
id:
  primary_key: true
```

```yaml title="Compound primary key" theme={null}
compound_primary_key_field:
  sql: concat(${field_1},'-',${field_2})
  primary_key: true
  hidden: true
```
