Search code examples
dbt

Why is DBT running a model that is not being targeted explicitly in the DBT run statement?


I have a DBT project that is mostly comprised of models for views over snowflake external tables.Every model view is triggered with a seperate dbt run statement concurrently.

dbt run --models model_for_view_1

I have one other model in the dbt project which materializes to a table that uses these views. I trigger this model in a separate DAG in airflow using the same DBT run statement as above. It uses no ref or source statement that connects it to the views.

I noticed recently that this table model is getting built by DBT whenever I build the view models. I thought it was because DBT was making an inference that this was a referenced model but after some experimentation in which I even set the table model SQL as something like SELECT 1+1 as column1, it was still getting built. I have placed it in a different folder in the dbt project, renamed the file etc. No joy. have no idea why running the other models is causing this unrelated model to be built. The only connection to the view models is that they share the same schema in the database. What is triggering this model to be built?


Solution

  • Selection syntax can be finicky, because there are many ways to select the models. From the docs:

    The --select flag accepts one or more arguments. Each argument can be one of:

    1. a package name
    2. a model name
    3. a fully-qualified path to a directory of models
    4. a selection method (path:, tag:, config:, test_type:, test_name:)

    (note that --models was renamed --select in v0.21, but --models has the same behavior)

    So my guess is that your model_for_view_1 name isn't unique, and is either shared with your project (acting as a package in this case) or the directory that it is in.

    So if your project looks like:

    models
     |- some_name
        |- some_name.sql  # the view
        |- another_name.sql  # the table
    

    dbt run --models some_name will run the code in both some_name.sql and another_name.sql, since it is selecting the directory called some_name.