Use more than one key as unique_key

Can I use multiple keys as unique_key in config?
e.g. Something like this: unique_key = ['date_sk', 'campaign_id', 'message_type', 'variation_name']

Hey @nicor88! Great question. unique_key accepts any valid SQL. So, you could do:

unique_key = 'field_1 || field_2 || field_3'

…or something similar. Generally, what we recommend is for all models to include a unique key in the contents of the model itself. This helps with uniqueness testing so that you can avoid fanout errors in your SQL. If there is no natural key in the underlying data and you need to create a surrogate key, I’d recommend using the surrogate key macro in dbt-utils to create one. Then you can easily pass that field into the unique_key config.

Hello. I have looked for an answer to a problem similar to this in Slack bu have had no luck. In the schema.yml file, immediately after the model name, I am testing for uniqueness using:

  - name: model1
    description: Does cools stuff.
        column_name: "concat(text_var, CAST(integer_var AS VARCHAR))"

Even if I simply use a column name I get an error when trying to compile. The error is:

  • Invalid arguments passed to “UnparsedSourceDefinition” instance:
    tables.4.tests.{’-unique’: {‘column_name’: ‘The stuff in the concat statement above’}} is not of type ‘array’

The documentation says to pass a valid SQL string. Any clues as to why this doesn’t work?


As Emily Lettela would say: “Nevermind”. I didn’t have a space between the “-” and the word “unique”.

Thanks for the vine.