Search⌘ K
AI Features

Using Macros from a Package

Explore how to integrate macros from external dbt packages by configuring the macro-paths list and learn to override package macros using dispatch settings. This lesson guides you through managing macro conflicts and customizing behavior for specific adapters, improving your project's modularity and flexibility.

Adding a package to the macro-paths list

After running dbt deps, we might need to use the log_info macro from the dbt-utils package. This macro allows us to log a formatted message to the command line. If we try to call that macro directly, like this:

MySQL
{{ log_info("This is just a dummy model") }}
SELECT 1

We get a compilation error:

Compilation Error in model other (models/other.sql) 'log_info' is undefined.
This can happen when calling a macro that does not exist.
Check for typos and/or install package dependencies with "dbt deps".

This is because dbt does not know it can look for macros in that package.

To change that, we need to edit the macro-paths list in the dbt_project.yml file, and add the path to the macros folder in the dbt-utils package.

YAML
# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'shop_project'
version: '1.0.0'
config-version: 2
# This setting configures which "profile" dbt uses for this project.
profile: 'shop'
# These configurations specify where dbt should look for different types of files.
# The `model-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros", "dbt_packages/dbt_utils/macros"]
snapshot-paths: ["snapshots"]
target-path: "target" # directory which will store compiled SQL files
clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"

This time, our log works: ...

16:28:05 2 of 2 START sql view model educative.other .............................. [RUN]
16:28:05 16:28:05 + This is just a dummy model
16:28:06 2 of 2 OK created sql view model educative.other .............................. [CREATE VIE
W (0 processed) in 0.91s]
Output of the dbt run command
...