Skip to content

Tagging Models in dbt

What are tags?

Tags in dbt are metadata annotations that can be applied to various resources such as models, tests, sources, snapshots, seeds, and metrics. They serve as labels that help you group and manage resources for selective execution, documentation, and workflow control.

Why use tags?

I am sure there are tons of reasons to use tags in dbt. So far, I have used them to …

  • Tell dbt where in Snowflake to write different models based on the environment. I applied a tag to my staging models folder and then referenced the tag in a macro. I’ll write up the details of this and hyperlink here.
  • Run specific models based on tags using these commands….
    • dbt run –select tag:tagname –> Runs tagged models
    • dbt run –select tag:tagname+ –> Runs tagged models and their children
    • dbt run –select +tag: tagname –> Runs tagged models and their parents
    • dbt run –select +tag: tagname+ –> Runs tagged models, parents, and children.
    • dbt run –select @tag: tagname –> Runs tagged models, all parents, all children, and parents of children
    • dbt run –exclude tag: tagname –> Excludes tagged models from execution

NOTE: When I was working on this, AI gave me commands using “dbt run –models”. However, note that “models” is deprecated, and you should use the “–select” command instead.

What is tag syntax?

First, know that tags need to be strings. To be considered a string, you can use single quotes, double quotes or no quotes. In the screenshot above, I used “+tags: [‘STAGING’]. Over the course of writing this up, I also tested out the following, and they all worked.

  • +tags: [‘STAGING’]
  • +tags: [“STAGING”]
  • +tags: STAGING

The only exception is when you put a tag inside of a model file. For more details on that, read on.

How or where do you tag models?

Models can be tagged in 3 places.

  1. The model file (shown below)
  2. The project.yml file (shown above)
  3. Model specific .yml files

Depending on your use case, it might make sense to tag in the model file. For my use case, I wanted to apply tags to all of the models in a particular folder without having to tag each one. Therefore, it made sense to apply tagging in the project.yml file, which is what is shown above. If you are going to tag in the model file, it is done like this. Just know that if you tag here, it only applies to that model.

Note that when you apply tags in the model file, the last option with no quotes will not work. That is because strings in jinja must be quoted. Without quotes, jinja interprets it as a variable, not a string. You can get away with not using quotes in the .yml file because YAML treats unquoted words as strings by default, unless they conflict with reserved keywords or contain special characters.

  • {{config(tags=’test’) }}
  • {{config(tags=”test”) }}
  • But NOT {{config(tags=test) }}

I’ll update this post when I find more uses for tags!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.