Access model name with jinja inside the model

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

Example:
My model is named: stg_source__model_name.sql

I would like something like:
{{ model.name }}
that returns
stg_source__model_name

Thanks!

Would {{ this.table }} meet your needs? https://docs.getdbt.com/reference/dbt-jinja-functions/this

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

model.name 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).

This is no longer an issue in the Cloud IDE: as long as your model is saved when you compile it, it will compile in the actual context and model.name will be correctly converted to its value

can u try using {{ this.name }}

{{ this.name }} → 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 }}. {{ this.name }}.

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.

Thanks!

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?

Thanks!

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

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.