# Update a column if table exists

**URL:** <https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923>\
**Category:** Archive\
**Created:** [February 26, 2022, 4:14am UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923 "2022-02-26T04:14:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vatsav](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@vatsav](https://discourse.getdbt.com/u/vatsav)\
**Post date:** [February 26, 2022, 4:14am UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923/1 "2022-02-26T04:14:17Z")

</div>

Hi,  
I am running an incremental model in DBT as per my requirement but I have to take care of one extra step where I have to do an update before my main query block is executed.  
I can use pre\_hook to do an update which will solve my problem but when i run this incremental model for the first time in DBT, the table would not have existed and my pre\_hook would fail.  
Any suggestion on how I can handle this scenario, by enabling my pre\_hook only after checking if the table structure exists would be helpful.

---

<div class="post-metadata">

**Author:** ![flira](https://avatars.discourse-cdn.com/v4/letter/f/7ea924/32.png) [@flira](https://discourse.getdbt.com/u/flira)\
**Post date:** [February 26, 2022, 9:56pm UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923/2 "2022-02-26T21:56:29Z")

</div>

What if in your pre-hook you call a macro with the is\_incremental() function inside of it?

---

<div class="post-metadata">

**Author:** ![ScottT](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@ScottT](https://discourse.getdbt.com/u/ScottT)\
**Post date:** [March 3, 2022, 7:18pm UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923/3 "2022-03-03T19:18:51Z")

</div>

I had a similar situation doing a delete before reloading. You can add the following code to your pre-hook macro. It defines a relation that can then be assessed for whether it exists:

```auto
{%- set target_relation = adapter.get_relation(
      database=this.database,
      schema=this.schema,
      identifier=this.name) -%}
{%- set table_exists=target_relation is not none -%}
{%- if table_exists -%}

  [do your update here]

{%- endif -%}

```

---

<div class="post-metadata">

**Author:** ![vatsav](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@vatsav](https://discourse.getdbt.com/u/vatsav)\
**Post date:** [March 5, 2022, 1:46pm UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923/4 "2022-03-05T13:46:59Z")

</div>

Thanks @ScottT I will try that out

---

<div class="post-metadata">

**Author:** ![Naunghton](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Naunghton](https://discourse.getdbt.com/u/Naunghton)\
**Post date:** [May 25, 2022, 9:58am UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923/5 "2022-05-25T09:58:26Z")

</div>

Hi @ScottT. Does this work in a prehook which is only computed at compilation? I seem to be having issues with this in a macro and a prehook

---

<div class="post-metadata">

**Author:** ![ScottT](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@ScottT](https://discourse.getdbt.com/u/ScottT)\
**Post date:** [May 25, 2022, 8:42pm UTC](https://discourse.getdbt.com/t/update-a-column-if-table-exists/3923/6 "2022-05-25T20:42:07Z")

</div>

Yes, it would be the basis of a macro called by a pre or post hook. I know there are limitations as to when things are compiled versus what can be dynamic. A reference could be passed rather than using this if that helps.
