# can dbt indicate primary keys

**URL:** <https://discourse.getdbt.com/t/can-dbt-indicate-primary-keys/11064>\
**Category:** Help\
**Created:** [December 6, 2023, 8:30am UTC](https://discourse.getdbt.com/t/can-dbt-indicate-primary-keys/11064 "2023-12-06T08:30:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![woody](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@woody](https://discourse.getdbt.com/u/woody)\
**Post date:** [December 6, 2023, 8:30am UTC](https://discourse.getdbt.com/t/can-dbt-indicate-primary-keys/11064/1 "2023-12-06T08:30:19Z")

</div>

## The problem I’m having

environment: Postgres  
when I using “dbt run” to the table,  
dbt will delete old table (I already dbt run before and alter PK).  
Is there any way to indicate PK in models?

## The context of why I’m trying to do this

I using airbyte to sync db table, when data sync it will run dbt and remove old table then create new one.

## What I’ve already tried

I know dbt can setting index in model (see: [Postgres configurations | dbt Developer Hub](https://docs.getdbt.com/reference/resource-configs/postgres-configs#indexes))

---

<div class="post-metadata">

**Author:** ![alyk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/alyk/32/3347_2.png) [@alyk](https://discourse.getdbt.com/u/alyk)\
**Post date:** [December 6, 2023, 2:17pm UTC](https://discourse.getdbt.com/t/can-dbt-indicate-primary-keys/11064/2 "2023-12-06T14:17:07Z")

</div>

Do you have v1.5+? Have you tried dbt [contract](https://docs.getdbt.com/reference/resource-configs/contract)/[constraints](https://docs.getdbt.com/reference/resource-properties/constraints)? dbt’s example yml:

```auto
models:
  - name: dim_customers
    config:
      contract:
        enforced: true
    columns:
      - name: id
        data_type: int
        constraints:
          - type: not_null
          - type: primary_key

```

Another idea would be to add the needed `alter table XXXXX add primary key...` statement as a [post-hook](https://docs.getdbt.com/reference/resource-configs/pre-hook-post-hook) to your model. Then it’d get executed with every dbt run.

Otherwise, you can indicate the _functionality_ of a PK in the yml by adding unique/not null tests (or [dbt\_expectations](https://hub.getdbt.com/calogica/dbt_expectations/latest/) `expect_compound_columns_to_be_unique` for a multi column PK)

---

<div class="post-metadata">

**Author:** ![woody](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@woody](https://discourse.getdbt.com/u/woody)\
**Post date:** [December 7, 2023, 9:53am UTC](https://discourse.getdbt.com/t/can-dbt-indicate-primary-keys/11064/3 "2023-12-07T09:53:25Z")

</div>

Thanks alyk  
Both constraints and post-hook are work!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [December 14, 2023, 9:53am UTC](https://discourse.getdbt.com/t/can-dbt-indicate-primary-keys/11064/4 "2023-12-14T09:53:30Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
