using doc macro in yml file is not executed on macro

The problem I’m having

I am using the doc macro in my YML file.
I need to use the column definition (retrieve the doc() result) into a custom macro, but the doc is undefined.

The context of why I’m trying to do this

I would like to generate the SQL file from the YML file.

Because I do not want to copy/paste the description of my columns between the different layers, I am using the doc() macro.

What I’ve already tried

Some example code or error messages

this is my YML file :

models:
  - name: gl_source_name
    description: "Model desc from external tool"
    config:
      meta:
        Approver: "something" # Or use an email address
        data_domain: "Generic"

    meta:
      semantic_view:
        tables:
          - name: FACTSOMETHING
            ref: gl_source_factsomething
            primary_key: [business_key, ...]
            columns:
              - name: business_key
                type: fact
                description: '{{ doc("business_key") }}'
                data_type: string
                synonyms: [syn1, syn2]
              - name: other_column
                type: fact
                description: '{{ doc("other_column_desc") }}'
...

my macro:

{% macro generate_view(sv) %}
..
    {%- set facts = [] %}
    {%- for table in sv.tables %}
    {%- for col in table.columns if col.type == 'fact' %}
    {%- do facts.append({'table': table.name, 'col': col}) %}
    {%- endfor %}
    {%- endfor %}
    {%- for item in facts %}
    {{ item.table }}.{{ item.col.name }} as {{ item.col.expr }} WITH SYNONYMS = ({% for syn in item.col.synonyms %}'{{ syn }}'{{ ", " if not loop.last }}{% endfor %}) COMMENT = '{{ item.col.description }}'{{ "," if not loop.last }}
    {%- endfor %}
..
{% endmacro %}

this part of the code is the issue :

COMMENT = '{{ item.col.description }}'

How I run it :

dbt run-operation generate_semantic_view

this is the output

Compilation Error
Could not render {{ doc(“business_key”) }}: ‘doc’ is undefined

This issue might be identical.

Is there any update on this ?

Rendering Macro within Macro - Help - dbt Community Forum

Multiple things:

  1. Can you share your dbt_project.yml? Do you have the docs-path defined? It is not being defined by default in there: docs-paths | dbt Developer Hub.
  2. What if you let dbt handle the docs persisting via persist_docs and just updated the table with the synonyms?

Hello,

Thank you for the quick answer.

this is the requested file.

name: 'dbt_sandbox'
version: '1.0.0'
profile: 'snow_db'
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]
docs-paths: ["docs"]
clean-targets:
  - "target"
  - "dbt_packages"

models:
  dbt_sandbox:

    +post-hook:
      - > # customize below `if` condition following your need
        {% if flags.WHICH in ('run', 'build') %}
          {{ dbt_tags.apply_column_tags() }}
        {% endif %}

    +persist_docs:
      relation: true
      columns: true

    bronze:
      +schema: bronze
      +materialized: table
      +tags: 
        - Bronze

    silver:
      +schema: silver
      +materialized: table
      +tags: 
        - Silver

    gold:
      +schema: gold
      +materialized: table
      +tags: 
        - Gold

seeds:
  dbt_sandbox:
    +persist_docs:
      relation: true
      columns: true

    reference:
      +schema: reference

vars:
  surrogate_key_treat_nulls_as_empty_strings: true

then I do have

./docs/descriptions.md

....
{% docs business_key %}
Surrogate business key .....
{% enddocs %}
....

This is not part of my use case, I really need to add the description on the COMMENT section of my generated SQL.

I would like to add the column description from the YML file on the comment section of the “semantic view” object from snowflake.

Btw, the persist_docs works well for my other use cases :slight_smile:

I would like to add the column description from the YML file on the comment section of the “semantic view” object from snowflake.

Ok get that now. So you are basically generating views as a posthook (?) and not as a model, thats why the persist docs doesn’t work - sorry.

Can this somehow fit in within the solution: dbt - Package hub?

I would be surprised and try adding the if execute condition About execute variable | dbt Developer Hub.

To be honest in that case im not 100 % sure how dbt handles the passing of the function or where.

You are right I am using it like a posthook (I do run it manually at the moment)

This Execute is only here to tell us if we are in parsing mode or run mode, right?
This won’t “execute” anything. right?

And yes I am already using this package in my project.

You are pointing something really interesting there: docs are not working when calling a posthook?

There is no way to use the doc() in my case?

Regards,

This Execute is only here to tell us if we are in parsing mode or run mode, right?
This won’t “execute” anything. right?
Yes exactly.

I’m not saying that docs are not working when calling a posthook, im saying im unfamiliar with when the docs are passed to the COMMENTs and how (because what you are trying to achieve is basically a persist docs thing), im just wondering when the queries are generated, I feel like they are passed for the specific model directly, it’s probably not like it gets replaced in the YML and then generated (if you see what I’m trying to say).

To clarify my request I have added the way I do run my macro.

Might be a better way to call it, and being able to get the doc() related to each fields.

I would be more than happy to update my macro.

Regards,

Apparently known issue from the Snowflake-Labs

support persist_docs to document dimension/fact/metric descriptions · Issue #12 · Snowflake-Labs/dbt_semantic_view