Dbt model insert query

I have a dbt model which references previous model and I use ref of previous model. The model has two steps. First step is to insert a few columns into another table from the previous model. Second step is a select query to select all columns from previous model. The problem is, when I run the model for the very first time, it gives compilation error for this insert query with the error saying that previous model name doesn’t exist or not authorized. It runs fine if I run this model by commenting the insert query. It creates a view based on select query. Now the view exists, and I uncomment the insert query, and it runs without any issue from the subsequent runs.

Now, the same issue is happening if previous model has some structural changes expected in the next run. It again throws a different error if previous model going for a metadata change. It throws another error - invalid number of result columns for set operator input branches, expected x got y in branch 2.

Any resolution? I added a logic to check if previous model exists and only insert if model exists to handle first scenario To bypass compilation error. But not sure how to handle second scenario. Any help is appreciated.


You could try using a pre_hook to “create or replace table” before the model runs. At least it will exist, even if empty

But previous model is a view which doesn’t have a pre defined structure, which gets recreated every time model runs. The structure can change

Post hook insert instead of pre hook?