# Access model name with jinja inside the model

**URL:** <https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487>\
**Category:** Help\
**Tags:** jinja\
**Created:** [March 27, 2023, 12:23pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487 "2023-03-27T12:23:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreinon](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/dreinon/32/2094_2.png) [@dreinon](https://discourse.getdbt.com/u/dreinon)\
**Post date:** [March 27, 2023, 12:23pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/1 "2023-03-27T12:23:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [March 27, 2023, 12:27pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/2 "2023-03-27T12:27:04Z")

</div>

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

Note: `@Owen` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1679920017302959?thread_ts=1679919838.252609&cid=CBSQTAPLG). It might not have transferred perfectly.

---

<div class="post-metadata">

**Author:** ![dreinon](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/dreinon/32/2094_2.png) [@dreinon](https://discourse.getdbt.com/u/dreinon)\
**Post date:** [March 27, 2023, 12:38pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/3 "2023-03-27T12:38:15Z")

</div>

It doesn’t, since in an incremental model, this compiles to `request`

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [March 28, 2023, 5:49am UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/4 "2023-03-28T05:49:17Z")

</div>

[model.name should work!](https://docs.getdbt.com/reference/dbt-jinja-functions/model)

~~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

---

<div class="post-metadata">

**Author:** ![Surya](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/surya/32/3763_2.png) [@Surya](https://discourse.getdbt.com/u/Surya)\
**Post date:** [March 28, 2023, 10:19am UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/5 "2023-03-28T10:19:01Z")

</div>

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 }}.`

---

<div class="post-metadata">

**Author:** ![dreinon](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/dreinon/32/2094_2.png) [@dreinon](https://discourse.getdbt.com/u/dreinon)\
**Post date:** [March 28, 2023, 2:02pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/6 "2023-03-28T14:02:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [March 28, 2023, 10:36pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/7 "2023-03-28T22:36:45Z")

</div>

> [@dreinon](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![dreinon](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/dreinon/32/2094_2.png) [@dreinon](https://discourse.getdbt.com/u/dreinon)\
**Post date:** [March 28, 2023, 11:00pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/8 "2023-03-28T23:00:23Z")

</div>

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](mailto:notifications@getdbt.discoursemail.com):

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [March 28, 2023, 11:16pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/9 "2023-03-28T23:16:46Z")

</div>

Yep! Check out [GitHub - dbt-labs/dbt-server: A web API for dbt.](https://github.com/dbt-labs/dbt-server)

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [April 4, 2023, 11:17pm UTC](https://discourse.getdbt.com/t/access-model-name-with-jinja-inside-the-model/7487/10 "2023-04-04T23:17:29Z")

</div>

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