# How to alias the model being built only?

**URL:** <https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069>\
**Category:** Help\
**Tags:** alias\
**Created:** [April 28, 2023, 11:30am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069 "2023-04-28T11:30:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![evgeniy](https://avatars.discourse-cdn.com/v4/letter/e/ecccb3/32.png) [@evgeniy](https://discourse.getdbt.com/u/evgeniy)\
**Post date:** [April 28, 2023, 11:30am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069/1 "2023-04-28T11:30:57Z")

</div>

Hello!

Is it possible to automatically alias only the model that is being built (using generate\_alias\_name) and not all models?

For example, if I run `dbt run -m model_name` I want `model_name` model to have database identifier as `dbtuser_model_name` so multiple data analysts can build the same model in the playground schema without collisions?

Basically I want to conditionally override the name of the model. Is it possible to extract some JInja variable that tells that this or that model is a model that is being build (not ref’ed).

---

<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:** [April 29, 2023, 7:04am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069/2 "2023-04-29T07:04:02Z")

</div>

pass a variable form dbt command and return alias name based on the variable in generate\_alias\_name macro

`dbt run -m model_name --vars 'add_database_to_model_name: true'`

```auto
{% macro generate_alias_name(custom_alias_name=none, node=none) -%}

    {%- if custom_alias_name is none -%}

        {{ node.name }}

    {%- elif var(add_database_to_model_name, false) == true -%}

        {{ "dbtuser_{}".fromat(custom_alias_name ) }}

    {%- else -%}
        {{ custom_alias_name | trim }}
    {%- endif -%}

{%- endmacro %}

```

---

<div class="post-metadata">

**Author:** ![evgeniy](https://avatars.discourse-cdn.com/v4/letter/e/ecccb3/32.png) [@evgeniy](https://discourse.getdbt.com/u/evgeniy)\
**Post date:** [April 29, 2023, 9:05am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069/3 "2023-04-29T09:05:26Z")

</div>

> [@Surya](#):
>
> `custom_alias_name`

Thank you for your reply.

In this case all tables will be aliased with `dbtuser_` prefix (not only the model that is being built). For example if we build `target_model` that references `source_model` with `{{ ref('source_model')` then `dbt run -m target_model --vars 'add_database_to_model_name: true'` will fail with the error that there is no table in the database with `dbtuser_source_model` identifier.

Is there a way to not alias source tables but only the model that is being built?

---

<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:** [April 30, 2023, 10:53am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069/4 "2023-04-30T10:53:31Z")

</div>

@evgeniy  
I understood ur requirement

This can be achieved using `invocation_args_dict` jinja variable and `node.name` in the `generate_alias_name` macro.

I have tried with the below code, looks like its working

```auto
{% macro generate_alias_name(custom_alias_name=none, node=none) -%}

    {%- if node.name in invocation_args_dict['select'] -%}

        {%- set database_identifier = 'dbtuser_{}'.format(node.name) -%}
        {{ database_identifier }}
        
    {%- elif custom_alias_name is none -%}

        {{ node.name }}

    {%- else -%}

        {{ custom_alias_name | trim }}

    {%- endif -%}

{%- endmacro %}

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/8/890f374f1385268f2362dc0c08eb965c4d2ce933.png)

---

<div class="post-metadata">

**Author:** ![evgeniy](https://avatars.discourse-cdn.com/v4/letter/e/ecccb3/32.png) [@evgeniy](https://discourse.getdbt.com/u/evgeniy)\
**Post date:** [May 3, 2023, 9:24am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069/5 "2023-05-03T09:24:47Z")

</div>

Wow, this is advanced. It works! Thank you!

---

<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:** [May 10, 2023, 9:25am UTC](https://discourse.getdbt.com/t/how-to-alias-the-model-being-built-only/8069/6 "2023-05-10T09:25:24Z")

</div>

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