From 95318705c629091c819d8e22cb2e1ec2b8caaf7c Mon Sep 17 00:00:00 2001 From: mirnawong1 Date: Tue, 10 Dec 2024 15:18:59 +0000 Subject: [PATCH] update unique_key --- website/docs/reference/model-configs.md | 15 +- .../reference/resource-configs/unique_key.md | 144 +++++++++++++++++- website/sidebars.js | 2 +- 3 files changed, 151 insertions(+), 10 deletions(-) diff --git a/website/docs/reference/model-configs.md b/website/docs/reference/model-configs.md index 9508cf68ceb..6c37b69758c 100644 --- a/website/docs/reference/model-configs.md +++ b/website/docs/reference/model-configs.md @@ -36,9 +36,11 @@ models: [+](/reference/resource-configs/plus-prefix)[materialized](/reference/resource-configs/materialized): [+](/reference/resource-configs/plus-prefix)[sql_header](/reference/resource-configs/sql_header): [+](/reference/resource-configs/plus-prefix)[on_configuration_change](/reference/resource-configs/on_configuration_change): apply | continue | fail #only for materialized views on supported adapters + [+](/reference/resource-configs/plus-prefix)[unique_key](/reference/resource-configs/unique_key): ``` + @@ -57,6 +59,7 @@ models: [materialized](/reference/resource-configs/materialized): [sql_header](/reference/resource-configs/sql_header): [on_configuration_change](/reference/resource-configs/on_configuration_change): apply | continue | fail #only for materialized views on supported adapters + [unique_key](/reference/resource-configs/unique_key): ``` @@ -69,12 +72,13 @@ models: -```jinja +```sql {{ config( [materialized](/reference/resource-configs/materialized)="", [sql_header](/reference/resource-configs/sql_header)="" [on_configuration_change](/reference/resource-configs/on_configuration_change): apply | continue | fail #only for materialized views for supported adapters + [unique_key](/reference/resource-configs/unique_key)='column_name_or_expression' ) }} ``` @@ -212,7 +216,7 @@ models: -```jinja +```sql {{ config( [enabled](/reference/resource-configs/enabled)=true | false, @@ -233,7 +237,7 @@ models: -```jinja +```sql {{ config( [enabled](/reference/resource-configs/enabled)=true | false, @@ -246,8 +250,9 @@ models: [persist_docs](/reference/resource-configs/persist_docs)={}, [meta](/reference/resource-configs/meta)={}, [grants](/reference/resource-configs/grants)={}, - [contract](/reference/resource-configs/contract)={} - [event_time](/reference/resource-configs/event-time): my_time_field + [contract](/reference/resource-configs/contract)={}, + [event_time](/reference/resource-configs/event-time)='my_time_field', + ) }} ``` diff --git a/website/docs/reference/resource-configs/unique_key.md b/website/docs/reference/resource-configs/unique_key.md index 77c99937295..344296dc134 100644 --- a/website/docs/reference/resource-configs/unique_key.md +++ b/website/docs/reference/resource-configs/unique_key.md @@ -1,12 +1,65 @@ --- -resource_types: [snapshots] +resource_types: [snapshots, models] description: "Learn more about unique_key configurations in dbt." datatype: column_name_or_expression --- + + + + +Configure the `unique_key` in the `config` block of your [incremental model](/docs/build/incremental-models) SQL file, in your `models/properties.yml` file, or in your `dbt_project.yml` file. + + + +```sql +{{ + config( + materialized='incremental', + unique_key='id' + ) +}} + +``` + + + + + +```yaml +models: + - name: my_incremental_model + description: "An incremental model example with a unique key." + config: + materialized: incremental + unique_key: id + +``` + + + + + +```yaml +name: jaffle_shop + +models: + jaffle_shop: + staging: + +unique_key: id +``` + + + + + + + +For [snapshots](/docs/build/snapshots), configure the `unique_key` in the your `snapshot/filename.yml` file or in your `dbt_project.yml` file. + ```yaml @@ -23,6 +76,8 @@ snapshots: +Configure the `unique_key` in the `config` block of your snapshot SQL file or in your `dbt_project.yml` file. + import SnapshotYaml from '/snippets/_snapshot-yaml-spec.md'; @@ -49,10 +104,13 @@ snapshots: + + + ## Description -A column name or expression that is unique for the inputs of a snapshot. dbt uses this to match records between a result set and an existing snapshot, so that changes can be captured correctly. +A column name or expression that is unique for the inputs of a snapshot or incremental model. dbt uses this to match records between a result set and an existing snapshot or incremental model, so that changes can be captured correctly. -In dbt Cloud "Latest" and dbt v1.9+, [snapshots](/docs/build/snapshots) are defined and configured in YAML files within your `snapshots/` directory. You can specify one or multiple `unique_key` values within your snapshot YAML file's `config` key. +In dbt Cloud "Latest" release track and from dbt v1.9, [snapshots](/docs/build/snapshots) are defined and configured in YAML files within your `snapshots/` directory. You can specify one or multiple `unique_key` values within your snapshot YAML file's `config` key. :::caution @@ -67,6 +125,32 @@ This is a **required parameter**. No default is provided. ## Examples ### Use an `id` column as a unique key + + + + +In this example, we use an `id` column as a unique key for an incremental model. + + + +```sql +{{ + config( + materialized='incremental', + unique_key='id' + ) +}} + +select * from .. +``` + + + + + + +In this example, we use an `id` column as a unique key for a snapshot. + @@ -114,10 +198,38 @@ snapshots: + + + ### Use multiple unique keys + + + +Configure multiple unique keys for an incremental model as a string representing a single column or a list of single-quoted column names that can be used together, for example, `['col1', 'col2', …]`. + +Columns must not contain null values, otherwise the incremental model will fail to match rows and generate duplicate rows. Refer to [Defining a unique key](/docs/build/incremental-models#defining-a-unique-key-optional) for more information. + + + +```sql +{{ config( + materialized='incremental', + unique_key=['order_id', 'location_id'] +) }} + +with... + +``` + + + + + + + You can configure snapshots to use multiple unique keys for `primary_key` columns. @@ -137,12 +249,35 @@ snapshots: ``` + + ### Use a combination of two columns as a unique key + + + + + +```sql +{{ config( + materialized='incremental', + unique_key=['order_id', 'location_id'] +) }} + +with... + +``` + + + + + + + This configuration accepts a valid column expression. As such, you can concatenate two columns together as a unique key if required. It's a good idea to use a separator (for example, `'-'`) to ensure uniqueness. @@ -170,7 +305,6 @@ from {{ source('erp', 'transactions') }} Though, it's probably a better idea to construct this column in your query and use that as the `unique_key`: - ```sql @@ -211,4 +345,6 @@ from {{ source('erp', 'transactions') }} ``` + + diff --git a/website/sidebars.js b/website/sidebars.js index 08494e4c713..5600fe164f2 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -941,6 +941,7 @@ const sidebarSettings = { "reference/resource-configs/pre-hook-post-hook", "reference/resource-configs/schema", "reference/resource-configs/tags", + "reference/resource-configs/unique_key", "reference/resource-configs/meta", "reference/advanced-config-usage", "reference/resource-configs/plus-prefix", @@ -985,7 +986,6 @@ const sidebarSettings = { "reference/resource-configs/strategy", "reference/resource-configs/target_database", "reference/resource-configs/target_schema", - "reference/resource-configs/unique_key", "reference/resource-configs/updated_at", ], },