# How to call a model from a macro

**URL:** https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140
**Category:** Help
**Tags:** macro
**Created:** [July 14, 2023, 7:46am UTC](https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140 "2023-07-14T07:46:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![janbenisek](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@janbenisek](https://discourse.getdbt.com/u/janbenisek)
#### Post date: [July 14, 2023, 7:46am UTC](https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140/1 "2023-07-14T07:46:06Z")

</div>

## The problem I’m having

I’d like to execute a particular model in a macro. Something along this way:

```auto
{%- macro foobar(model_name, target_name) -%}

dbt run -s {{ model_name }} -t {{ target_name }}

{%- endmacro -%}

```

## The context of why I’m trying to do this

We want to help others running models safely in a dev schema. However, some models have many large upstream dependencies. Therefore, I am writing a macro which, given a model name I want to refresh, copies all production tables and views, basically all upstream models, into a dev schema.

I am able to get all the upstream models. Copying `tables` is also straightforward (`create table dev_john.big_table as select * from prod_schema.big_table`), however there is no such thing for a `view`.

Therefore, I am looking for a way to run models which are views from macro.

(I know about `--defer` and we use it, but we do not want to use it in this case as it sometimes leads to annoying transaction blocks and problems)

## What I’ve already tried

The only thing I came up is to just `create view dev_john.my_view as select * from prod_schema.my_view with no schema binding`. But I’d rather to do `dbt run -s my_view -t dev_john` to really have no dependencies between prod and dev.

Any ideas would be greatly appreciated.  
Thank you.

---

<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: [July 14, 2023, 12:13pm UTC](https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140/2 "2023-07-14T12:13:42Z")

</div>

Not a direct answer, but would the “upstream-prod” package help you? [https://hub.getdbt.com/lewisdavies/upstream\_prod/latest/](https://hub.getdbt.com/lewisdavies/upstream_prod/latest/)

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

---

<div class="post-metadata">

### Author: ![janbenisek](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@janbenisek](https://discourse.getdbt.com/u/janbenisek)
#### Post date: [July 14, 2023, 12:35pm UTC](https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140/3 "2023-07-14T12:35:07Z")

</div>

Thanks! I saw that one. From what I understand, this is basically `--defer` without the artefacts.

It is close, but it reads production tables, while I need complete isolation. That is, having the prod tables in dev.

---

<div class="post-metadata">

### Author: ![naik.manjunath90](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/naik.manjunath90/32/3547_2.png) [@naik.manjunath90](https://discourse.getdbt.com/u/naik.manjunath90)
#### Post date: [January 18, 2024, 6:23am UTC](https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140/4 "2024-01-18T06:23:44Z")

</div>

Any update on above requirement?i would like to run dbt model using macro

---

<div class="post-metadata">

### Author: ![janbenisek](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@janbenisek](https://discourse.getdbt.com/u/janbenisek)
#### Post date: [January 18, 2024, 8:43am UTC](https://discourse.getdbt.com/t/how-to-call-a-model-from-a-macro/9140/5 "2024-01-18T08:43:09Z")

</div>

For myself, I simply get all the model info from dbt artifacts and then iterate over them like that:

```auto
{%- for upstream_model, configs in model_properties.items() -%}

      {{ dbt_utils.log_info("drop table if exists " ~ dev_schema ~ "." ~ configs['alias'] ~ " cascade;" ) }}
      {% do run_query("drop table if exists " ~ dev_schema ~ "." ~ configs['alias'] ~ " cascade;" ) %}

      {{ dbt_utils.log_info("begin; create table " ~ dev_schema ~ "." ~ configs['alias'] ~ " as select * from " ~ configs['schema'] ~ "." ~ configs['alias'] ~ " " ~ limit ~ "; commit;") }}
      {% do run_query("begin; create table " ~ dev_schema ~ "." ~ configs['alias'] ~ " as select * from " ~ configs['schema'] ~ "." ~ configs['alias'] ~ " " ~ limit ~ "; commit;") %}

```

Not perfect, but it works.  
For my use-case, there is also now `dbt clone` which could be useful.  
However, haven’t found any way to run a model by `dbt run -s foo -t prod` in a macro.
