# Weird behaviour of pre\_hook

**URL:** <https://discourse.getdbt.com/t/weird-behaviour-of-pre-hook/9386>\
**Category:** Help\
**Tags:** jinja, bigquery, dbt-cloud, macros\
**Created:** [August 2, 2023, 2:04pm UTC](https://discourse.getdbt.com/t/weird-behaviour-of-pre-hook/9386 "2023-08-02T14:04:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZH6](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/zh6/32/2817_2.png) [@ZH6](https://discourse.getdbt.com/u/ZH6)\
**Post date:** [August 2, 2023, 2:04pm UTC](https://discourse.getdbt.com/t/weird-behaviour-of-pre-hook/9386/1 "2023-08-02T14:04:15Z")

</div>

I would like to run a DELETE statement before the execution of my model. My model is called `ga_events`. The DELETE statement should be based on another model called `stg_events_inc`. There fore I created a macro:

```auto
{%- macro get_delete_statement(table, stg_table) -%}
    DELETE FROM {{ table }} WHERE event_date BETWEEN (SELECT MIN(event_date) FROM {{ stg_table }}) AND (SELECT MAX(event_date) FROM {{ stg_table }});
{%- endmacro -%}

```

I’m calling the macro from the `ga_events` model in the config block, like so:

```auto
{{
    config(
        materialized = 'incremental',
        pre_hook = get_delete_statement(this, ref('stg_events_inc')),
        on_schema_change = 'fail'
    )
}}

```

The final compilated DELETE statement looks like this:

```auto
DELETE FROM `project`.`schema`.`ga_events` WHERE event_date BETWEEN (SELECT MIN(event_date) FROM `project`.`schema`.`ga_events`) AND (SELECT MAX(event_date) FROM `project`.`schema`.`ga_events`);

```

I cannot understand why it completely ignores the stg\_events\_inc reference in the WHERE clause.

---

<div class="post-metadata">

**Author:** ![ZH6](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/zh6/32/2817_2.png) [@ZH6](https://discourse.getdbt.com/u/ZH6)\
**Post date:** [August 3, 2023, 10:03am UTC](https://discourse.getdbt.com/t/weird-behaviour-of-pre-hook/9386/2 "2023-08-03T10:03:35Z")

</div>

> [@ZH6](#):
>
> `get_delete_statement(this, ref('stg_events_inc'))`

If this is not in the `config` block it works normally. Is it a bug or it is supposed to work that way?

---

<div class="post-metadata">

**Author:** ![ZH6](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/zh6/32/2817_2.png) [@ZH6](https://discourse.getdbt.com/u/ZH6)\
**Post date:** [August 22, 2023, 4:23pm UTC](https://discourse.getdbt.com/t/weird-behaviour-of-pre-hook/9386/3 "2023-08-22T16:23:57Z")

</div>

It is an issue as you can see here: [using `{{ ref('anything') }}` in a `set_sql_header` call resolves to the current model and not 'anything' · Issue #2793 · dbt-labs/dbt-core · GitHub](https://github.com/dbt-labs/dbt-core/issues/2793)

Pretty sad, that it has been there for almost 3 years and the issue persists.
