# dbt model cannot be refered in the same model

**URL:** <https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938>\
**Category:** Help\
**Created:** [April 20, 2023, 8:15pm UTC](https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938 "2023-04-20T20:15:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![callmedivs](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@callmedivs](https://discourse.getdbt.com/u/callmedivs)\
**Post date:** [April 20, 2023, 8:15pm UTC](https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938/1 "2023-04-20T20:15:04Z")

</div>

## The problem I’m having

Hi, I’m new to dbt and  
I’m having a problem referencing the same table creating the model

```auto
I'm trying to multiple inserts into this table say tableA.
So this is a sample of the code i have

select distinct 1 as x1,
a.col1 as x2
from tableA a

  union

select 
distinct 1 as x1, 
cr.col5 as x2
from tableA cr
and not exists (
  select 1
  from {{ this }} m
  where m.x1 = 1
  AND m.x2 = cr.x2
  )

```

Error:  
19:56:47 Opening a new connection, currently in state closed  
19:56:48 Snowflake adapter: Snowflake query id: 01abc28c-0402-d621-005b-8a830196377e  
19:56:48 Snowflake adapter: Snowflake error: 002003 (42S02): SQL compilation error:  
Object ‘DEV.DW.tableB’ does not exist or not authorized.

Any advice?

---

<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:** [April 20, 2023, 8:52pm UTC](https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938/2 "2023-04-20T20:52:04Z")

</div>

You say that your sample code is for tableA, but the error message is

> [@callmedivs](#):
>
> Object ‘DEV.DW.tableB’ does not exist or not authorized.

Are you `ref`ing that model but haven’t built it yet, or aren’t granting permissions to it? I don’t see where in your code tableB is being used, maybe if you’re doing a `dbt run` without any selection logic this is actually an error in a different model.

---

<div class="post-metadata">

**Author:** ![callmedivs](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@callmedivs](https://discourse.getdbt.com/u/callmedivs)\
**Post date:** [April 20, 2023, 9:03pm UTC](https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938/3 "2023-04-20T21:03:49Z")

</div>

Hey Joelabes, tableA is created in another model and already exists. And tableB is the filename of this the model I’m trying to run now and get the error. The error is complaning about tableB, because i’m refering it with {{ this }}} in the where clause in the second select stmt

---

<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:** [April 26, 2023, 6:30am UTC](https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938/4 "2023-04-26T06:30:08Z")

</div>

Ahh OK! On your first run, the table doesn’t exist.

The standard solution for this is to use the [is\_incremental() macro](https://docs.getdbt.com/docs/build/incremental-models#understanding-the-is_incremental-macro). It will return false unless the table alrady exists (and you’re not running a `--full-refresh`- check the doc for the full rundown).

You need to write your model in such a way that it works whether or not the table already exists, to enable dbt to run in an [idempotent](https://docs.getdbt.com/terms/idempotent) way.

---

<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:** [July 19, 2023, 3:42am UTC](https://discourse.getdbt.com/t/dbt-model-cannot-be-refered-in-the-same-model/7938/5 "2023-07-19T03:42:03Z")

</div>

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