# {{ schema }} not returning custom schema

**URL:** <https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828>\
**Category:** Help\
**Tags:** jinja, dbt-cloud\
**Created:** [April 13, 2023, 6:17pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828 "2023-04-13T18:17:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [April 13, 2023, 6:17pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/1 "2023-04-13T18:17:18Z")

</div>

## The problem I’m having

`{{ schema }}` is not returning the schema defined in project.yml for this model’s folder. It is instead returning the default target schema; the same as `{{ target.schema }}` would. This isn’t the behaviour I’d expect from reading the [documentation](https://docs.getdbt.com/reference/dbt-jinja-functions/schema).

nb. I’m overriding `generate_schema_name()` so that a custom schema is used without the default schema as a prefix.

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

My model builds to a custom schema, and needs to reference a UDF in that same schema.

## What I’ve already tried

`{{ schema }}`  
`{{ target.schema }}`

## Some example code or error messages

1, Create a model in a subfolder e.g. “reporting”  
2, Define the custom schema for this folder in project.yml

```auto
    reporting:
      schema: reporting

```

3, Override the dbt schema generator ; create macros\generate\_schema\_name:

```auto
{% macro generate_schema_name(custom_schema_name, node) -%}

    {%- set default_schema = target.schema -%}
    {%- if custom_schema_name is none -%}

        {{ default_schema }}

    {%- else -%}

        {{ custom_schema_name | trim }}

    {%- endif -%}

{%- endmacro %}

```

---

<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 14, 2023, 4:05am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/2 "2023-04-14T04:05:04Z")

</div>

may i know the dbt version your using.  
Could you also share your project.yml file and schema.yml file  
please check if u are using schema config in your schema.yml and model.sql file

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [April 14, 2023, 8:01am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/3 "2023-04-14T08:01:48Z")

</div>

I’m using dbt cloud.

My schema.yml file is blank ; I intend to use it for tests later on:

```auto
version: 2

```

project.yml I define the schema at model-folder level (it’s the gold schema in this case):

```auto

# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'catalyst_model'
version: '1.0.0'
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: 'default'

# These configurations specify where dbt should look for different types of files.
# The `source-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target" # directory which will store compiled SQL files
clean-targets: # directories to be removed by `dbt clean`
  - "target"
  - "dbt_packages"

models:
  catalyst_model:
    archive:
      +enabled: false
    gold:
      schema: gold
      materialized: table
    staging:
      schema: dbt_staging
      materialized: table

```

However the use of `{{ schema }}` in any syntax in a model within the `gold` folder is generating `dbt_staging`, which is my default target schema. It still generates the model in `gold`, correctly.

---

<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 14, 2023, 9:36am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/4 "2023-04-14T09:36:19Z")

</div>

> [@Surya](#):
>
> may i know the dbt version your using.

may i know the dbt version you’re using.

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [April 14, 2023, 9:41am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/5 "2023-04-14T09:41:12Z")

</div>

cloud, or do you mean something else

---

<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 14, 2023, 9:49am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/6 "2023-04-14T09:49:19Z")

</div>

in your logs you could see something like  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/5/53968228d61c24d61e9e2753a4b5020b23166972.png)

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [April 14, 2023, 2:34pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/7 "2023-04-14T14:34:34Z")

</div>

Cloud logs work differently & don’t show the version ; you’re using CLI there I take it? I think cloud is always on the latest dbt runtime.

---

<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 17, 2023, 3:45am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/8 "2023-04-17T03:45:51Z")

</div>

can u try this` {{model['schema']}}`

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [May 3, 2023, 1:56pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/9 "2023-05-03T13:56:35Z")

</div>

Hiya - still giving me the default schema, instead of the one configured in project.yml

---

<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:** [May 4, 2023, 7:09am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/10 "2023-05-04T07:09:34Z")

</div>

Thank you for the clear reproduction steps!

The docs you linked to do say that `{{ schema }}` should be the same as `{{ model['schema'] }}`, but would you mind trying that as well? It’s odd to me that the model is being built in the right place but the jinja variable is returning the incorrect value.

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [May 4, 2023, 8:23am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/11 "2023-05-04T08:23:55Z")

</div>

> [@joellabes](#):
>
> {{ model[‘schema’] }}

Hiya - yeah I’ve tried this too. Actually there’s a much easier replication method ; just compile the selection within a model like this. These should all say “gold” in my case, since that’s the project config.

![Capture1](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/e/e42666e4a9a06ff1654fb3fb6435a95a96d13a95.png)

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [May 4, 2023, 9:08am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/12 "2023-05-04T09:08:21Z")

</div>

I’ve just tested it without the custom schema macro (override), same issue. So it’s actually quite a simple thing to replicate, dunno if anyone else wants to try it?

---

<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:** [May 4, 2023, 12:41pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/13 "2023-05-04T12:41:16Z")

</div>

can you show us the dbt\_project.yml file

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [May 4, 2023, 1:19pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/14 "2023-05-04T13:19:02Z")

</div>

```auto

# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'catalyst_model'
version: '1.0.0'
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: 'default'

# These configurations specify where dbt should look for different types of files.
# The `source-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target" # directory which will store compiled SQL files
clean-targets: # directories to be removed by `dbt clean`
  - "target"
  - "dbt_packages"

models:
  catalyst_model:
    archive:
      +enabled: false
    gold:
      schema: gold
      materialized: table
    staging:
      schema: dbt_staging
      materialized: table
    reporting:
      schema: reporting
      materialized: view

```

---

<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:** [May 4, 2023, 1:26pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/15 "2023-05-04T13:26:47Z")

</div>

final check  
can u add this **{{ this.schema }}** in one of the models in gold folder and check.

Is this happening only to one model in the gold folder? or is it happening to all models in the gold folder

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [May 4, 2023, 1:31pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/16 "2023-05-04T13:31:01Z")

</div>

Same issue - yeah, happening in every model in the folder.

---

<div class="post-metadata">

**Author:** ![thesaint](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@thesaint](https://discourse.getdbt.com/u/thesaint)\
**Post date:** [May 4, 2023, 3:46pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/17 "2023-05-04T15:46:39Z")

</div>

I am experiencing something like this as well running dbt-sqlserver, core 1.5… My models are not accepting my custom schema and materialization while spitting out the default schema and materialization

---

<div class="post-metadata">

**Author:** ![janine.rawnsley](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janine.rawnsley/32/2207_2.png) [@janine.rawnsley](https://discourse.getdbt.com/u/janine.rawnsley)\
**Post date:** [May 9, 2023, 2:29pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/18 "2023-05-09T14:29:40Z")

</div>

Should this be raised as a bug?

---

<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:** [May 9, 2023, 10:17pm UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/19 "2023-05-09T22:17:58Z")

</div>

OK I have just had a chance to try this out properly; sorry for the delay!

It behaves correctly in the CLI:

```yml
# dbt_project.yml
models:
  +materialized: view
  schema: alternate_schema

```

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

But in dbt Cloud previews it shows the incorrect behaviour as you showed above.

> [@janine.rawnsley](#):
>
> ![Capture1](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/e/e42666e4a9a06ff1654fb3fb6435a95a96d13a95.png)

I wonder if this might be an issue with the soon-to-be-replaced `dbt-rpc` library which currently powers the IDE experience - I’ll ask around and update this thread if I hear anything.

---

<div class="post-metadata">

**Author:** ![jkpltd](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jkpltd/32/2085_2.png) [@jkpltd](https://discourse.getdbt.com/u/jkpltd)\
**Post date:** [May 10, 2023, 1:54am UTC](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828/20 "2023-05-10T01:54:10Z")

</div>

I have same issue.

It works fine when I click “Build”, but gives wrong value when I click “Preview”

My workaround was to create a variable with the default schema and then pass that to the “generate\_schema\_name” macro.

```
 {{ generate_schema_name(var('udf-schema'))}}.fn_json_array_value__to_csv_string(i.field) ,

```

[Next page](https://discourse.getdbt.com/t/schema-not-returning-custom-schema/7828.md?page=2)
