How to get current folder name based on {{this.xx}}

Hi,
planed to create a macro and capture current model folder name, is that possible? i can get schema, name, by {{this.schema}},but is it possible to get current model’ folder name? have multiple subfolder under model.
Anyone can help on this?
Thanks,
ALEX

Hi Alex,
I am not sure there is folder information but you might look into the graph object and nodes / models.
I am not an expert in this area, but I hope it can help.

Ciao, Roberto

Hi Roberto, not too sure i followed what are you referring on graph object and nodes / models , is this some kind of attribute on {{this}}? like this.node? can you be specific? thanks

Thanks,
Alex

Hey Alex, I may be wrong on this but dbt doesn’t really understand folders. The models (.sql files) are compiled in a single namespace and are acesses as nodes in a graph (DAG). If you want to access the model name, “this” should do it. Please see examples of post-hook operations where grants are being executed on {{ this }}.
Does this help?

You can access this information using the model’s node representation within the dbt graph.

The model node is available from:

  1. An argument to generate_schema_name if you are creating a custom schema
    • This is applicable if you are overriding the generate_schema_name macro in your project
    • In this case you could retrieve the path’s subdirectory from the node's “fully-qualified name” or fqn attribute, which is a list of the path, pre-split for you:
{%- macro generate_schema_name(custom_schema_name, node) -%}
   {% log(node.fqn, info=True) %}
   {# other stuff ... #}
{{ endmacro }}
  1. The dbt graph’s node dictionary
    • This is applicable if you need to access the the node information in an arbitrary macro using the graph function
    • You can access a model’s node in the graph using the nodes dictionary
      • the key for the nodes dictionary is the model’s unique id, which is also retrievable from the model object with model.get('unique_id') if you are calling a macro within the scope of a dbt invocation that is model-scoped (e.g. dbt run -m somemodel or dbt compile -m somemodel)
{% set nodes = graph.get('nodes', ({})) %}
{% set node = nodes.get('model_node_id') -%}
{{ log(node.path, info=True) }}
1 Like

thanks so much. i will try on this.

i am looking for the model path like folder name of this model files rather than the model name. but thanks still.