Access model name with jinja inside the model

I would like to use my model name in its query using jinja. Is this possible?

My model is named: stg_source__model_name.sql

I would like something like:
{{ }}
that returns


Would {{ this.table }} meet your needs?

Note: @Owen originally posted this reply in Slack. It might not have transferred perfectly.

It doesn’t, since in an incremental model, this compiles to request should work!

It looks like you might be using the dbt cloud IDE - the rpc server that powers it will sometimes return request instead of the node name (normally when in an unsaved file).

can u try using {{ }}

{{ }} → current_model name

{{ this.database }} → current model database name

{{ this.schema }} → current model schema name

{{ this.identifier }} → cureent_model name

{{ this }} -> {{ this.database }}.{{ this.schema }}. {{ }}.

Thanks for answering @joellabes @Surya!
Right, since I am using the cloud IDE, it always returns request instead of the model name when using preview and compile, but does return the correct model name on build.

Is there a way to make it work for preview too? Otherwise I cannot debug it and preview it while developing.


Not at the moment unfortunately - it’s a limitation of the old dbt-rpc project. The good news is that we are actively working to replace dbt-rpc with a new system that will be able to do this correctly - this doesn’t help you right now but is very front of mind for us.

Nice @joellabes!
Is there any issue or PR that describes this new dbt-rpc so I can follow it?


miércoles, 29 marzo 2023, 00:46a. m. +02:00 de Joel Labes via dbt Community Forum

Yep! Check out GitHub - dbt-labs/dbt-server: A web API for dbt.

We’re expecting this to land in the IDE somewhere around the middle of this year :crossed_fingers:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.