# How to only build models that passed tests in the previous build?

**URL:** <https://discourse.getdbt.com/t/how-to-only-build-models-that-passed-tests-in-the-previous-build/6467>\
**Category:** Help\
**Tags:** testing, bigquery, deferral-and-state\
**Created:** [January 31, 2023, 10:52am UTC](https://discourse.getdbt.com/t/how-to-only-build-models-that-passed-tests-in-the-previous-build/6467 "2023-01-31T10:52:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thmsrey](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@Thmsrey](https://discourse.getdbt.com/u/Thmsrey)\
**Post date:** [January 31, 2023, 10:52am UTC](https://discourse.getdbt.com/t/how-to-only-build-models-that-passed-tests-in-the-previous-build/6467/1 "2023-01-31T10:52:14Z")

</div>

Hi everyone,

I’m trying a new approach for data quality in our warehouse where we would build models on an audit schema and then build again on a prod schema, excluding models that failed the tests during the audit build.

For the 2nd build I tried with the command  
`dbt build --exclude 1+result:fail` as mentioned in the doc but I’m getting the following error:

```auto
Internal Error
  No comparison run_results

```

which I don’t really understand since I actually have a run\_results.json file stored in my `/target` folder.

`dbt build --exclude 1+result:fail --defer --state ./target` seems to work but I don’t really know why and I’m not sure it does the same thing, I still have to wrap my head around the defer method.

Does someone have an idea?

Thanks 🙂

---

<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:** [February 2, 2023, 1:32am UTC](https://discourse.getdbt.com/t/how-to-only-build-models-that-passed-tests-in-the-previous-build/6467/2 "2023-02-02T01:32:17Z")

</div>

~~Without providing the `--defer` flag, dbt doesn’t try to defer to an old version. Likewise,~~ without the `--state ` flag, it doesn’t know where to look for the comparison manifest.

> [@Thmsrey](#):
>
> `dbt build --exclude 1+result:fail --state ./target`

Is the correct command to run to successfully use the result method in your selector

---

<div class="post-metadata">

**Author:** ![matthiash](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@matthiash](https://discourse.getdbt.com/u/matthiash)\
**Post date:** [October 5, 2023, 2:06pm UTC](https://discourse.getdbt.com/t/how-to-only-build-models-that-passed-tests-in-the-previous-build/6467/3 "2023-10-05T14:06:10Z")

</div>

> [@joellabes](#):
>
> Without providing the `--defer` flag, dbt doesn’t try to defer to an old version. Likewise, without the `--state ` flag, it doesn’t know where to look for the comparison manifest.

But in the [docs](https://docs.getdbt.com/reference/node-selection/methods), this is given as an example:

`dbt run --select result:error --state path/to/artifacts # run all models that generated errors on the prior invocation of dbt run`

Why is `--defer` not used here?

---

<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:** [October 10, 2023, 10:32pm UTC](https://discourse.getdbt.com/t/how-to-only-build-models-that-passed-tests-in-the-previous-build/6467/4 "2023-10-10T22:32:03Z")

</div>

Oh yes you’re right! You only need to pass the `--state` flag. I’ve edited my post above
