Build a dbt Project with Claude Code (Claude for Data Engineers Part 5)

Claude for Data Engineers · Part 5 of 10

In this post we are going to cover how to build a complete dbt project with Claude Code, from raw seed files to tested marts, on a small online shop in DuckDB. We load the seeds, write staging and mart models, add tests that catch a real data problem, fix it in staging, and run dbt build until everything passes. The last step is generating the docs and writing the project rules into CLAUDE.md.

dbt is the tool many data teams use to build their warehouse models in SQL: each model is a select statement, and dbt builds them in the right order and runs tests on the results. Claude Code is Claude working inside your project folder. It reads your files, writes the models and runs dbt in your terminal, so it works from the real output, not a guess. Every dbt output in this post comes from really running it, with dbt-core 1.12.6 and the dbt-duckdb adapter.

Part 5 of the Claude for Data Engineers series on TechBrothersIT.

Let's create some sample data

Our project is shop-dbt. It has a dbt_project.yml, a profiles.yml that points at a local DuckDB file called shop.duckdb, a short CLAUDE.md, and three CSV seed files: 9 customer rows, 12 orders and 14 payments, with the payment amounts in cents. There are no models yet. One thing is hidden in the customers file on purpose, and the tests will find it later. Here are all the starting files.

dbt_project.yml
name: shop
version: '1.0'
profile: shop

model-paths: ['models']
seed-paths: ['seeds']
profiles.yml
shop:
  target: dev
  outputs:
    dev:
      type: duckdb
      path: shop.duckdb
      threads: 1
CLAUDE.md
# shop-dbt

dbt project for the online shop. Adapter: dbt-duckdb, database file `shop.duckdb` (see profiles.yml).

- Raw data: seeds/ (raw_customers, raw_orders, raw_payments)
- Layers: models/staging (stg_*), models/marts (fct_*, dim_*)
seeds/raw_customers.csv
id,first_name,last_name,email,country,updated_at
1,Sara,Khan,Sara.Khan@example.com,US,2026-01-04 09:12:00
2,Omar,Ali,omar.ali@example.com,UK,2026-01-09 14:30:00
3,Maya,Chen,maya.chen@example.com,CA,2026-02-02 08:05:00
4,Liam,Brooks,Liam.Brooks@example.com,US,2026-02-17 19:44:00
5,Ana,Silva,ana.silva@example.com,BR,2026-03-01 11:20:00
6,Noah,Weber,noah.weber@example.com,DE,2026-03-12 16:02:00
7,Zara,Ahmed,zara.ahmed@example.com,UK,2026-04-08 10:15:00
8,Ethan,Park,ethan.park@example.com,US,2026-04-21 13:37:00
3,Maya,Chen,maya.chen@example.com,US,2026-06-15 17:50:00
seeds/raw_orders.csv
id,user_id,order_date,status
101,1,2026-09-01,completed
102,3,2026-09-02,completed
103,2,2026-09-03,shipped
104,1,2026-09-05,returned
105,4,2026-09-06,completed
106,5,2026-09-08,placed
107,3,2026-09-10,completed
108,6,2026-09-11,shipped
109,7,2026-09-13,completed
110,8,2026-09-15,completed
111,3,2026-09-18,shipped
112,2,2026-09-20,completed
seeds/raw_payments.csv
id,order_id,payment_method,amount
1,101,credit_card,12900
2,102,paypal,4550
3,103,credit_card,8999
4,104,credit_card,3200
5,105,gift_card,2500
6,105,credit_card,7450
7,106,paypal,1999
8,107,credit_card,15800
9,108,bank_transfer,6400
10,109,credit_card,2275
11,110,paypal,9900
12,111,gift_card,5000
13,111,credit_card,3120
14,112,credit_card,4410

Step 1: Open the project and load the seeds

We open a terminal in the shop-dbt folder and start Claude Code. It asks if we trust this folder, and we say yes. Then we ask Claude to look at the project and load the seeds.

Prompt - type this in Claude Code
Look at this dbt project and load the seed files

Claude reads dbt_project.yml and profiles.yml first, so it knows the adapter and where the database lives. It tells us this is a dbt-duckdb project that writes to shop.duckdb, with three seed files and no models yet. Then it runs dbt seed, which loads each CSV file as a table.

Terminal - the command
dbt seed
Output
22:16:02  Running with dbt=1.12.6
22:16:07  Registered adapter: duckdb=1.11.0
22:16:08  Unable to do partial parsing because saved manifest not found. Starting full parse.
22:16:10  Found 3 seeds, 500 macros
22:16:10
22:16:10  Concurrency: 1 threads (target='dev')
22:16:10
22:16:13  1 of 3 START seed file main.raw_customers ...................................... [RUN]
22:16:13  1 of 3 OK loaded seed file main.raw_customers .................................. [INSERT 9 in 0.12s]
22:16:13  2 of 3 START seed file main.raw_orders ......................................... [RUN]
22:16:13  2 of 3 OK loaded seed file main.raw_orders ..................................... [INSERT 12 in 0.02s]
22:16:13  3 of 3 START seed file main.raw_payments ....................................... [RUN]
22:16:13  3 of 3 OK loaded seed file main.raw_payments ................................... [INSERT 14 in 0.02s]
22:16:13
22:16:13  Finished running 3 seeds in 0 hours 0 minutes and 3.11 seconds (3.11s).
22:16:13
22:16:13  Completed successfully
22:16:13
22:16:13  Done. PASS=3 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=3

Three seeds, three tables: INSERT 9 for raw_customers, INSERT 12 for raw_orders and INSERT 14 for raw_payments, and the run ends with PASS=3 and ERROR=0. Note that it says 9 customer rows, not 9 customers. Keep that in mind.

Tip

Seeds are great for small, static files like this demo. Real raw data should come in as sources, loaded by your pipeline.

Step 2: Staging models

Now the staging layer: one model per raw table, with clean names and clean types. Before we ask, we press Shift+Tab to switch from auto mode, the default, to manual mode, because we want to see each file before Claude writes it.

Prompt - type this in Claude Code
Create staging models for customers, orders and payments: rename columns, cast types, amounts from cents to dollars. Staging as views, marts as tables.

Claude says it will add one staging model per seed under models/staging, then set the materializations in dbt_project.yml. It asks to create the first file, and we choose Yes, allow all edits during this session. That switches the session to accept edits mode, so the other files go straight in. Then Claude wants to run dbt for the staging folder. File edits are allowed now, but commands still ask, so we choose Yes, and don't ask again for dbt run.

models/staging/stg_customers.sql
select
    id                            as customer_id,
    first_name,
    last_name,
    lower(email)                  as email,
    country,
    cast(updated_at as timestamp) as updated_at
from {{ ref('raw_customers') }}
models/staging/stg_orders.sql
select
    id                       as order_id,
    user_id                  as customer_id,
    cast(order_date as date) as order_date,
    status
from {{ ref('raw_orders') }}
models/staging/stg_payments.sql
select
    id                                      as payment_id,
    order_id,
    payment_method,
    cast(amount / 100.0 as decimal(10, 2)) as amount
from {{ ref('raw_payments') }}
dbt_project.yml
name: shop
version: '1.0'
profile: shop

model-paths: ['models']
seed-paths: ['seeds']

models:
  shop:
    staging:
      +materialized: view
    marts:
      +materialized: table
Terminal - the command
dbt run --select staging
Output
22:16:15  Running with dbt=1.12.6
22:16:15  Registered adapter: duckdb=1.11.0
22:16:15  Unable to do partial parsing because a project config has changed
22:16:17  [WARNING]: Configuration paths exist in your dbt_project.yml file which do not apply to any resources.
There are 1 unused configuration paths:
- models.shop.marts
22:16:17  Found 3 models, 3 seeds, 500 macros
22:16:17
22:16:17  Concurrency: 1 threads (target='dev')
22:16:17
22:16:17  1 of 3 START sql view model main.stg_customers ................................. [RUN]
22:16:17  1 of 3 OK created sql view model main.stg_customers ............................ [OK in 0.08s]
22:16:17  2 of 3 START sql view model main.stg_orders .................................... [RUN]
22:16:17  2 of 3 OK created sql view model main.stg_orders ............................... [OK in 0.03s]
22:16:17  3 of 3 START sql view model main.stg_payments .................................. [RUN]
22:16:17  3 of 3 OK created sql view model main.stg_payments ............................. [OK in 0.03s]
22:16:17
22:16:17  Finished running 3 view models in 0 hours 0 minutes and 0.24 seconds (0.24s).
22:16:17
22:16:17  Completed successfully
22:16:17
22:16:17  Done. PASS=3 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=3

In stg_customers, id becomes customer_id, the email goes to lower case and updated_at is cast to a timestamp. In stg_payments the amount is divided by 100 and cast to decimal(10, 2), so it is in dollars now. The new models: block in dbt_project.yml builds staging as views and marts as tables. The run creates three views with PASS=3. The warning about the unused path models.shop.marts is expected: there are no mart models yet.

Tip

Keep staging models boring. Rename, cast and clean, but no joins and no business logic.

Step 3: Marts and a first result

Next, the marts. These are the tables the business actually uses. We ask for a fact table of orders with the payment total, and a customer table with lifetime value.

Prompt - type this in Claude Code
Add marts: fct_orders with the payment total per order, and dim_customers with order count and lifetime value (skip returned orders)

Claude writes fct_orders first. It sums the payments per order in a CTE, then joins them to the orders, so an order with two payments still gives one row. Then dim_customers counts the orders per customer and sums their value, leaving returned orders out of the lifetime value. Then it runs dbt run, with no prompt this time, because of the rule we saved in step 2.

models/marts/fct_orders.sql
with orders as (
    select * from {{ ref('stg_orders') }}
),

payments as (
    select order_id, sum(amount) as amount, count(*) as payment_count
    from {{ ref('stg_payments') }}
    group by order_id
)

select
    o.order_id,
    o.customer_id,
    o.order_date,
    o.status,
    coalesce(p.amount, 0)        as amount,
    coalesce(p.payment_count, 0) as payment_count
from orders o
left join payments p on p.order_id = o.order_id
models/marts/dim_customers.sql
with customers as (
    select * from {{ ref('stg_customers') }}
),

orders as (
    select * from {{ ref('fct_orders') }}
)

select
    c.customer_id,
    c.first_name,
    c.last_name,
    c.country,
    count(o.order_id) as orders,
    coalesce(sum(o.amount) filter (where o.status <> 'returned'), 0) as lifetime_value,
    min(o.order_date) as first_order,
    max(o.order_date) as last_order
from customers c
left join orders o on o.customer_id = c.customer_id
group by all
Terminal - the command
dbt run
Output
22:16:19  Running with dbt=1.12.6
22:16:19  Registered adapter: duckdb=1.11.0
22:16:20  Found 5 models, 3 seeds, 500 macros
22:16:20
22:16:20  Concurrency: 1 threads (target='dev')
22:16:20
22:16:20  1 of 5 START sql view model main.stg_customers ................................. [RUN]
22:16:20  1 of 5 OK created sql view model main.stg_customers ............................ [OK in 0.10s]
22:16:20  2 of 5 START sql view model main.stg_orders .................................... [RUN]
22:16:20  2 of 5 OK created sql view model main.stg_orders ............................... [OK in 0.04s]
22:16:20  3 of 5 START sql view model main.stg_payments .................................. [RUN]
22:16:20  3 of 5 OK created sql view model main.stg_payments ............................. [OK in 0.03s]
22:16:20  4 of 5 START sql table model main.fct_orders ................................... [RUN]
22:16:20  4 of 5 OK created sql table model main.fct_orders .............................. [OK in 0.09s]
22:16:20  5 of 5 START sql table model main.dim_customers ................................ [RUN]
22:16:20  5 of 5 OK created sql table model main.dim_customers ........................... [OK in 0.03s]
22:16:20
22:16:20  Finished running 2 table models, 3 view models in 0 hours 0 minutes and 0.42 seconds (0.42s).
22:16:20
22:16:20  Completed successfully
22:16:20
22:16:20  Done. PASS=5 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=5

All five models build: PASS=5, two table models and three view models. Now Claude checks the new customer table with the DuckDB command line. Accept edits mode does not cover commands like this, so it asks, and we approve the query once with Yes.

Terminal - the command
duckdb shop.duckdb -c "SELECT customer_id, first_name, last_name, country, orders, lifetime_value FROM dim_customers ORDER BY lifetime_value DESC LIMIT 5"
Output
┌─────────────┬────────────┬───────────┬─────────┬────────┬────────────────┐
│ customer_id │ first_name │ last_name │ country │ orders │ lifetime_value │
│    int32    │  varchar   │  varchar  │ varchar │ int64  │ decimal(38,2)  │
├─────────────┼────────────┼───────────┼─────────┼────────┼────────────────┤
│           3 │ Maya       │ Chen      │ US      │      3 │         284.70 │
│           3 │ Maya       │ Chen      │ CA      │      3 │         284.70 │
│           2 │ Omar       │ Ali       │ UK      │      2 │         134.09 │
│           1 │ Sara       │ Khan      │ US      │      2 │         129.00 │
│           4 │ Liam       │ Brooks    │ US      │      1 │          99.50 │
└─────────────┴────────────┴───────────┴─────────┴────────┴────────────────┘

Look at the top two rows. Customer 3, Maya Chen, appears twice: once with US and once with CA, both with 3 orders and 284.70. Claude spots it too, and says raw_customers probably has two rows for that id, so her orders are counted twice. It suggests adding tests before going further.

This is the lesson from real projects: dbt run only tells you the SQL worked. It does not tell you the data is right.

Tip

The duckdb command is the DuckDB CLI. dbt-duckdb does not install it, so install it separately if you want to run these checks yourself.

Step 4: Tests that catch bad data

Now let's make dbt check the data for us. We ask for key tests, a list of accepted statuses, and relationship tests between the tables.

Prompt - type this in Claude Code
Add tests: unique + not_null on every primary key, accepted values for order status, relationships from orders to customers and payments to orders. Then run them.

Claude writes two schema files: _staging.yml for the staging models and _marts.yml for the marts. Each column lists its tests, and each model gets a short description, which dbt also uses for the docs. Then it wants to run dbt test, and we choose Yes, and don't ask again for that command.

models/staging/_staging.yml
version: 2

models:
  - name: stg_customers
    description: One row per customer, cleaned from raw_customers.
    columns:
      - name: customer_id
        description: Primary key.
        data_tests: [unique, not_null]

  - name: stg_orders
    description: One row per order.
    columns:
      - name: order_id
        data_tests: [unique, not_null]
      - name: customer_id
        data_tests:
          - relationships:
              arguments:
                to: ref('stg_customers')
                field: customer_id
      - name: status
        data_tests:
          - accepted_values:
              arguments:
                values: ['placed', 'shipped', 'completed', 'returned']

  - name: stg_payments
    description: One row per payment, amount in dollars.
    columns:
      - name: payment_id
        data_tests: [unique, not_null]
      - name: order_id
        data_tests:
          - relationships:
              arguments:
                to: ref('stg_orders')
                field: order_id
models/marts/_marts.yml
version: 2

models:
  - name: fct_orders
    description: One row per order with the total paid.
    columns:
      - name: order_id
        data_tests: [unique, not_null]

  - name: dim_customers
    description: One row per customer with order count and lifetime value (returned orders excluded).
    columns:
      - name: customer_id
        data_tests: [unique, not_null]
Terminal - the command
dbt test
Output (exit code 1, expected)
22:16:27  Running with dbt=1.12.6
22:16:27  Registered adapter: duckdb=1.11.0
22:16:28  Found 5 models, 3 seeds, 13 data tests, 500 macros
22:16:28
22:16:28  Concurrency: 1 threads (target='dev')
22:16:28
22:16:28  1 of 13 START test accepted_values_stg_orders_status__placed__shipped__completed__returned  [RUN]
22:16:28  1 of 13 PASS accepted_values_stg_orders_status__placed__shipped__completed__returned  [PASS in 0.04s]
22:16:28  2 of 13 START test not_null_dim_customers_customer_id .......................... [RUN]
22:16:28  2 of 13 PASS not_null_dim_customers_customer_id ................................ [PASS in 0.02s]
22:16:28  3 of 13 START test not_null_fct_orders_order_id ................................ [RUN]
22:16:28  3 of 13 PASS not_null_fct_orders_order_id ...................................... [PASS in 0.02s]
22:16:28  4 of 13 START test not_null_stg_customers_customer_id .......................... [RUN]
22:16:28  4 of 13 PASS not_null_stg_customers_customer_id ................................ [PASS in 0.02s]
22:16:28  5 of 13 START test not_null_stg_orders_order_id ................................ [RUN]
22:16:28  5 of 13 PASS not_null_stg_orders_order_id ...................................... [PASS in 0.02s]
22:16:28  6 of 13 START test not_null_stg_payments_payment_id ............................ [RUN]
22:16:28  6 of 13 PASS not_null_stg_payments_payment_id .................................. [PASS in 0.02s]
22:16:28  7 of 13 START test relationships_stg_orders_customer_id__customer_id__ref_stg_customers_  [RUN]
22:16:28  7 of 13 PASS relationships_stg_orders_customer_id__customer_id__ref_stg_customers_  [PASS in 0.02s]
22:16:28  8 of 13 START test relationships_stg_payments_order_id__order_id__ref_stg_orders_  [RUN]
22:16:28  8 of 13 PASS relationships_stg_payments_order_id__order_id__ref_stg_orders_ .... [PASS in 0.02s]
22:16:28  9 of 13 START test unique_dim_customers_customer_id ............................ [RUN]
22:16:28  9 of 13 FAIL 1 unique_dim_customers_customer_id ................................ [FAIL 1 in 0.02s]
22:16:28  10 of 13 START test unique_fct_orders_order_id ................................. [RUN]
22:16:28  10 of 13 PASS unique_fct_orders_order_id ....................................... [PASS in 0.02s]
22:16:28  11 of 13 START test unique_stg_customers_customer_id ........................... [RUN]
22:16:28  11 of 13 FAIL 1 unique_stg_customers_customer_id ............................... [FAIL 1 in 0.02s]
22:16:28  12 of 13 START test unique_stg_orders_order_id ................................. [RUN]
22:16:28  12 of 13 PASS unique_stg_orders_order_id ....................................... [PASS in 0.02s]
22:16:28  13 of 13 START test unique_stg_payments_payment_id ............................. [RUN]
22:16:28  13 of 13 PASS unique_stg_payments_payment_id ................................... [PASS in 0.01s]
22:16:28
22:16:28  Finished running 13 data tests in 0 hours 0 minutes and 0.36 seconds (0.36s).
22:16:29
22:16:29  Completed with 2 errors, 0 partial successes, and 0 warnings:
22:16:29
22:16:29  [ERROR]: in test unique_dim_customers_customer_id (models/marts/_marts.yml)
22:16:29    Got 1 result, configured to fail if != 0
22:16:29
22:16:29    compiled code at target/compiled/shop/models/marts/_marts.yml/unique_dim_customers_customer_id.sql
22:16:29
22:16:29  [ERROR]: in test unique_stg_customers_customer_id (models/staging/_staging.yml)
22:16:29    Got 1 result, configured to fail if != 0
22:16:29
22:16:29    compiled code at target/compiled/shop/models/staging/_staging.yml/unique_stg_customers_customer_id.sql
22:16:29
22:16:29  Done. PASS=11 WARN=0 ERROR=2 SKIP=0 NO-OP=0 REUSED=0 TOTAL=13

dbt found 13 data tests, and this time we get a real failure: PASS=11 and ERROR=2, and the command exits with code 1. The two failures are unique_stg_customers_customer_id and unique_dim_customers_customer_id, each with Got 1 result, configured to fail if != 0. That is the test doing its job. The accepted values and relationship tests all pass.

To find out why, Claude looks at the raw rows for customer 3. We approve the query once.

Terminal - the command
duckdb shop.duckdb -c "SELECT id, first_name, last_name, country, updated_at FROM raw_customers WHERE id = 3"
Output
┌───────┬────────────┬───────────┬─────────┬─────────────────────┐
│  id   │ first_name │ last_name │ country │     updated_at      │
│ int32 │  varchar   │  varchar  │ varchar │      timestamp      │
├───────┼────────────┼───────────┼─────────┼─────────────────────┤
│     3 │ Maya       │ Chen      │ CA      │ 2026-02-02 08:05:00 │
│     3 │ Maya       │ Chen      │ US      │ 2026-06-15 17:50:00 │
└───────┴────────────┴───────────┴─────────┴─────────────────────┘

Two rows for the same customer, exported twice. The older row, from 2026-02-02, says CA, and the newer one, from 2026-06-15, says US. Claude proposes to keep only the latest row per customer in stg_customers, so every mart gets the fix.

Tip

Duplicates like this usually come from a source system that sends updates. Fix them once, in staging, not in every mart.

Step 5: Fix it and run dbt build

Let's fix it where the problem starts, in the staging model. We tell Claude to go ahead.

Prompt - type this in Claude Code
Yes, dedupe in stg_customers (latest updated_at wins), then run dbt build

Claude edits stg_customers.sql and adds a qualify clause with row_number(), partitioned by id and ordered by updated_at newest first, so only row number 1 is kept. Then it wants to run dbt build, which runs the seeds, the models and the tests in dependency order. We choose Yes, and don't ask again, so dbt build is allowed for this project.

models/staging/stg_customers.sql
select
    id                            as customer_id,
    first_name,
    last_name,
    lower(email)                  as email,
    country,
    cast(updated_at as timestamp) as updated_at
from {{ ref('raw_customers') }}
-- raw_customers can hold the same customer more than once: keep the latest row
qualify row_number() over (partition by id order by updated_at desc) = 1
Terminal - the command
dbt build
Output
22:16:30  Running with dbt=1.12.6
22:16:31  Registered adapter: duckdb=1.11.0
22:16:32  Found 3 seeds, 5 models, 13 data tests, 500 macros
22:16:32
22:16:32  Concurrency: 1 threads (target='dev')
22:16:32
22:16:32  1 of 21 START seed file main.raw_customers ..................................... [RUN]
22:16:32  1 of 21 OK loaded seed file main.raw_customers ................................. [INSERT 9 in 0.09s]
22:16:32  2 of 21 START seed file main.raw_orders ........................................ [RUN]
22:16:32  2 of 21 OK loaded seed file main.raw_orders .................................... [INSERT 12 in 0.03s]
22:16:32  3 of 21 START seed file main.raw_payments ...................................... [RUN]
22:16:32  3 of 21 OK loaded seed file main.raw_payments .................................. [INSERT 14 in 0.02s]
22:16:32  4 of 21 START sql view model main.stg_customers ................................ [RUN]
22:16:32  4 of 21 OK created sql view model main.stg_customers ........................... [OK in 0.08s]
22:16:32  5 of 21 START sql view model main.stg_orders ................................... [RUN]
22:16:32  5 of 21 OK created sql view model main.stg_orders .............................. [OK in 0.03s]
22:16:32  6 of 21 START sql view model main.stg_payments ................................. [RUN]
22:16:32  6 of 21 OK created sql view model main.stg_payments ............................ [OK in 0.04s]
22:16:32  7 of 21 START test not_null_stg_customers_customer_id .......................... [RUN]
22:16:32  7 of 21 PASS not_null_stg_customers_customer_id ................................ [PASS in 0.04s]
22:16:32  8 of 21 START test unique_stg_customers_customer_id ............................ [RUN]
22:16:32  8 of 21 PASS unique_stg_customers_customer_id .................................. [PASS in 0.02s]
22:16:32  9 of 21 START test accepted_values_stg_orders_status__placed__shipped__completed__returned  [RUN]
22:16:32  9 of 21 PASS accepted_values_stg_orders_status__placed__shipped__completed__returned  [PASS in 0.05s]
22:16:32  10 of 21 START test not_null_stg_orders_order_id ............................... [RUN]
22:16:32  10 of 21 PASS not_null_stg_orders_order_id ..................................... [PASS in 0.02s]
22:16:32  11 of 21 START test relationships_stg_orders_customer_id__customer_id__ref_stg_customers_  [RUN]
22:16:32  11 of 21 PASS relationships_stg_orders_customer_id__customer_id__ref_stg_customers_  [PASS in 0.03s]
22:16:32  12 of 21 START test unique_stg_orders_order_id ................................. [RUN]
22:16:32  12 of 21 PASS unique_stg_orders_order_id ....................................... [PASS in 0.02s]
22:16:32  13 of 21 START test not_null_stg_payments_payment_id ........................... [RUN]
22:16:32  13 of 21 PASS not_null_stg_payments_payment_id ................................. [PASS in 0.02s]
22:16:32  14 of 21 START test relationships_stg_payments_order_id__order_id__ref_stg_orders_  [RUN]
22:16:32  14 of 21 PASS relationships_stg_payments_order_id__order_id__ref_stg_orders_ ... [PASS in 0.02s]
22:16:32  15 of 21 START test unique_stg_payments_payment_id ............................. [RUN]
22:16:32  15 of 21 PASS unique_stg_payments_payment_id ................................... [PASS in 0.02s]
22:16:32  16 of 21 START sql table model main.fct_orders ................................. [RUN]
22:16:33  16 of 21 OK created sql table model main.fct_orders ............................ [OK in 0.08s]
22:16:33  17 of 21 START test not_null_fct_orders_order_id ............................... [RUN]
22:16:33  17 of 21 PASS not_null_fct_orders_order_id ..................................... [PASS in 0.02s]
22:16:33  18 of 21 START test unique_fct_orders_order_id ................................. [RUN]
22:16:33  18 of 21 PASS unique_fct_orders_order_id ....................................... [PASS in 0.03s]
22:16:33  19 of 21 START sql table model main.dim_customers .............................. [RUN]
22:16:33  19 of 21 OK created sql table model main.dim_customers ......................... [OK in 0.04s]
22:16:33  20 of 21 START test not_null_dim_customers_customer_id ......................... [RUN]
22:16:33  20 of 21 PASS not_null_dim_customers_customer_id ............................... [PASS in 0.02s]
22:16:33  21 of 21 START test unique_dim_customers_customer_id ........................... [RUN]
22:16:33  21 of 21 PASS unique_dim_customers_customer_id ................................. [PASS in 0.02s]
22:16:33
22:16:33  Finished running 3 seeds, 2 table models, 13 data tests, 3 view models in 0 hours 0 minutes and 0.87 seconds (0.87s).
22:16:33
22:16:33  Completed successfully
22:16:33
22:16:33  Done. PASS=21 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=21

All green: PASS=21 and ERROR=0, for 3 seeds, 5 models and 13 tests. Look at the order: the staging tests run before fct_orders is built, and the fct_orders tests run before dim_customers. A failing test stops the bad data before it reaches the next model. Both unique tests that failed before, on stg_customers and dim_customers, pass now. Claude checks the customer table one more time, and we approve the query.

Terminal - the command
duckdb shop.duckdb -c "SELECT customer_id, first_name, last_name, country, orders, lifetime_value FROM dim_customers ORDER BY lifetime_value DESC LIMIT 5"
Output
┌─────────────┬────────────┬───────────┬─────────┬────────┬────────────────┐
│ customer_id │ first_name │ last_name │ country │ orders │ lifetime_value │
│    int32    │  varchar   │  varchar  │ varchar │ int64  │ decimal(38,2)  │
├─────────────┼────────────┼───────────┼─────────┼────────┼────────────────┤
│           3 │ Maya       │ Chen      │ US      │      3 │         284.70 │
│           2 │ Omar       │ Ali       │ UK      │      2 │         134.09 │
│           1 │ Sara       │ Khan      │ US      │      2 │         129.00 │
│           4 │ Liam       │ Brooks    │ US      │      1 │          99.50 │
│           8 │ Ethan      │ Park      │ US      │      1 │          99.00 │
└─────────────┴────────────┴───────────┴─────────┴────────┴────────────────┘

Maya Chen is there once, with US and 3 orders, and her lifetime value is still 284.70. The duplicate row is gone, so Ethan Park moves into the top five with 99.00.

If two rows in a real feed can have the same updated_at, add a second column to the order by, so the same row always wins.

Tip

Use this loop on every dbt change with Claude: change the model, run dbt build, read the result, and only then move on.

Step 6: Docs and CLAUDE.md

Last step: the docs, and teaching Claude the rules of this project.

Prompt - type this in Claude Code
Generate the dbt docs, then add the commands and the rules we used today to CLAUDE.md

We approve the docs command once. Then Claude edits CLAUDE.md and adds a Commands section and a Rules section.

CLAUDE.md
# shop-dbt

dbt project for the online shop. Adapter: dbt-duckdb, database file `shop.duckdb` (see profiles.yml).

- Raw data: seeds/ (raw_customers, raw_orders, raw_payments)
- Layers: models/staging (stg_*), models/marts (fct_*, dim_*)

## Commands
- `dbt build` runs seeds, models and tests in dependency order
- `dbt build --select stg_customers+` one model and everything downstream
- `dbt docs generate` after changing descriptions

## Rules
- Every model gets unique + not_null tests on its primary key
- Staging models: rename, cast, dedupe. No joins, no business logic
- Fix bad raw data in staging, not in each mart
- Run `dbt build` and make sure it passes before saying a change is done
Terminal - the command
dbt docs generate
Output
22:16:35  Running with dbt=1.12.6
22:16:35  Registered adapter: duckdb=1.11.0
22:16:36  Found 3 seeds, 5 models, 13 data tests, 500 macros
22:16:36
22:16:36  Concurrency: 1 threads (target='dev')
22:16:36
22:16:37  Building catalog
22:16:37  Catalog written to /home/aamir/shop-dbt/target/catalog.json

dbt docs generate builds the catalog, and the output ends with Catalog written to /home/aamir/shop-dbt/target/catalog.json. The descriptions from our two schema files become the docs. The new CLAUDE.md has three dbt commands, including dbt build --select stg_customers+ for one model and everything downstream, and four rules: key tests on every model, boring staging models, fix bad raw data in staging, and run dbt build before saying a change is done.

Claude Code reads this file at the start of every session in this folder. So next time it adds key tests to new models and runs dbt build before it says it is done, without us asking.

Tip

To browse the docs, run dbt docs serve in your own terminal. It starts a small web server and opens the docs in your browser.

Tips for dbt with Claude Code

Before we finish, here are the habits I use on a real dbt project.

DBT WITH CLAUDE CODE

Four habits that save you

  • CLAUDE.md: dbt commands and modeling rules
  • Pre-allow safe commands: Bash(dbt build *) in .claude/settings.json
  • Tests catch bad data, fix it once in staging
  • Dev target only, never prod. Read every diff

Put your dbt commands and your modeling rules in CLAUDE.md, so every session starts with them. Pre-allow the safe commands, like dbt build, in your project's .claude/settings.json, and keep everything else on ask. Let the tests catch bad data, and fix it once in staging. And point Claude at a local or dev target, never at production, and read every diff before you accept it.

Key points to remember

  • Seeds loaded into DuckDB
  • Staging models (views)
  • Marts: fct_orders, dim_customers
  • Tests caught a real duplicate
  • Fixed in staging, dbt build all green
  • Docs and CLAUDE.md rules

The full script

This bash script writes every file Claude wrote, in the same order, and runs every command, so you can reproduce all the results without Claude.

Show the full script (every file and every command, in order)
Full script - bash, run it in an empty folder
#!/usr/bin/env bash
# Claude for Data Engineers #5: Build a dbt Project with Claude Code
# Rebuilds the demo project without Claude: writes every file Claude wrote, in the same order,
# and runs every command from the video. Run it in an empty folder.
# Tools used in the video: Python 3.13, DuckDB 2.0.0, pandas 3.0.5, PySpark 4.3.0 (local mode, Java 21),
# dbt-core 1.12.6 with dbt-duckdb 1.11.0, PostgreSQL 16. Install the ones this project uses first.
mkdir -p shop-dbt && cd shop-dbt
export DBT_PROFILES_DIR="$PWD"      # profiles.yml is in the project folder

# ---------- starting files ----------
cat > dbt_project.yml <<'CLAUDE_DE_EOF'
name: shop
version: '1.0'
profile: shop

model-paths: ['models']
seed-paths: ['seeds']
CLAUDE_DE_EOF
cat > profiles.yml <<'CLAUDE_DE_EOF'
shop:
  target: dev
  outputs:
    dev:
      type: duckdb
      path: shop.duckdb
      threads: 1
CLAUDE_DE_EOF
cat > CLAUDE.md <<'CLAUDE_DE_EOF'
# shop-dbt

dbt project for the online shop. Adapter: dbt-duckdb, database file `shop.duckdb` (see profiles.yml).

- Raw data: seeds/ (raw_customers, raw_orders, raw_payments)
- Layers: models/staging (stg_*), models/marts (fct_*, dim_*)
CLAUDE_DE_EOF
mkdir -p seeds
cat > seeds/raw_customers.csv <<'CLAUDE_DE_EOF'
id,first_name,last_name,email,country,updated_at
1,Sara,Khan,Sara.Khan@example.com,US,2026-01-04 09:12:00
2,Omar,Ali,omar.ali@example.com,UK,2026-01-09 14:30:00
3,Maya,Chen,maya.chen@example.com,CA,2026-02-02 08:05:00
4,Liam,Brooks,Liam.Brooks@example.com,US,2026-02-17 19:44:00
5,Ana,Silva,ana.silva@example.com,BR,2026-03-01 11:20:00
6,Noah,Weber,noah.weber@example.com,DE,2026-03-12 16:02:00
7,Zara,Ahmed,zara.ahmed@example.com,UK,2026-04-08 10:15:00
8,Ethan,Park,ethan.park@example.com,US,2026-04-21 13:37:00
3,Maya,Chen,maya.chen@example.com,US,2026-06-15 17:50:00
CLAUDE_DE_EOF
mkdir -p seeds
cat > seeds/raw_orders.csv <<'CLAUDE_DE_EOF'
id,user_id,order_date,status
101,1,2026-09-01,completed
102,3,2026-09-02,completed
103,2,2026-09-03,shipped
104,1,2026-09-05,returned
105,4,2026-09-06,completed
106,5,2026-09-08,placed
107,3,2026-09-10,completed
108,6,2026-09-11,shipped
109,7,2026-09-13,completed
110,8,2026-09-15,completed
111,3,2026-09-18,shipped
112,2,2026-09-20,completed
CLAUDE_DE_EOF
mkdir -p seeds
cat > seeds/raw_payments.csv <<'CLAUDE_DE_EOF'
id,order_id,payment_method,amount
1,101,credit_card,12900
2,102,paypal,4550
3,103,credit_card,8999
4,104,credit_card,3200
5,105,gift_card,2500
6,105,credit_card,7450
7,106,paypal,1999
8,107,credit_card,15800
9,108,bank_transfer,6400
10,109,credit_card,2275
11,110,paypal,9900
12,111,gift_card,5000
13,111,credit_card,3120
14,112,credit_card,4410
CLAUDE_DE_EOF

# ---------- step 1: Load the seed files into DuckDB ----------
dbt seed

# ---------- step 2: Staging models: rename, cast, cents to dollars ----------
mkdir -p models/staging
cat > models/staging/stg_customers.sql <<'CLAUDE_DE_EOF'
select
    id                            as customer_id,
    first_name,
    last_name,
    lower(email)                  as email,
    country,
    cast(updated_at as timestamp) as updated_at
from {{ ref('raw_customers') }}
CLAUDE_DE_EOF
mkdir -p models/staging
cat > models/staging/stg_orders.sql <<'CLAUDE_DE_EOF'
select
    id                       as order_id,
    user_id                  as customer_id,
    cast(order_date as date) as order_date,
    status
from {{ ref('raw_orders') }}
CLAUDE_DE_EOF
mkdir -p models/staging
cat > models/staging/stg_payments.sql <<'CLAUDE_DE_EOF'
select
    id                                      as payment_id,
    order_id,
    payment_method,
    cast(amount / 100.0 as decimal(10, 2)) as amount
from {{ ref('raw_payments') }}
CLAUDE_DE_EOF
cat > dbt_project.yml <<'CLAUDE_DE_EOF'
name: shop
version: '1.0'
profile: shop

model-paths: ['models']
seed-paths: ['seeds']

models:
  shop:
    staging:
      +materialized: view
    marts:
      +materialized: table
CLAUDE_DE_EOF
dbt run --select staging

# ---------- step 3: Marts: fct_orders and dim_customers ----------
mkdir -p models/marts
cat > models/marts/fct_orders.sql <<'CLAUDE_DE_EOF'
with orders as (
    select * from {{ ref('stg_orders') }}
),

payments as (
    select order_id, sum(amount) as amount, count(*) as payment_count
    from {{ ref('stg_payments') }}
    group by order_id
)

select
    o.order_id,
    o.customer_id,
    o.order_date,
    o.status,
    coalesce(p.amount, 0)        as amount,
    coalesce(p.payment_count, 0) as payment_count
from orders o
left join payments p on p.order_id = o.order_id
CLAUDE_DE_EOF
mkdir -p models/marts
cat > models/marts/dim_customers.sql <<'CLAUDE_DE_EOF'
with customers as (
    select * from {{ ref('stg_customers') }}
),

orders as (
    select * from {{ ref('fct_orders') }}
)

select
    c.customer_id,
    c.first_name,
    c.last_name,
    c.country,
    count(o.order_id) as orders,
    coalesce(sum(o.amount) filter (where o.status <> 'returned'), 0) as lifetime_value,
    min(o.order_date) as first_order,
    max(o.order_date) as last_order
from customers c
left join orders o on o.customer_id = c.customer_id
group by all
CLAUDE_DE_EOF
dbt run

# ---------- step 4: Check the customer mart ----------
duckdb shop.duckdb -c "SELECT customer_id, first_name, last_name, country, orders, lifetime_value FROM dim_customers ORDER BY lifetime_value DESC LIMIT 5"

# ---------- step 5: Add tests and run dbt test (two tests fail) ----------
mkdir -p models/staging
cat > models/staging/_staging.yml <<'CLAUDE_DE_EOF'
version: 2

models:
  - name: stg_customers
    description: One row per customer, cleaned from raw_customers.
    columns:
      - name: customer_id
        description: Primary key.
        data_tests: [unique, not_null]

  - name: stg_orders
    description: One row per order.
    columns:
      - name: order_id
        data_tests: [unique, not_null]
      - name: customer_id
        data_tests:
          - relationships:
              arguments:
                to: ref('stg_customers')
                field: customer_id
      - name: status
        data_tests:
          - accepted_values:
              arguments:
                values: ['placed', 'shipped', 'completed', 'returned']

  - name: stg_payments
    description: One row per payment, amount in dollars.
    columns:
      - name: payment_id
        data_tests: [unique, not_null]
      - name: order_id
        data_tests:
          - relationships:
              arguments:
                to: ref('stg_orders')
                field: order_id
CLAUDE_DE_EOF
mkdir -p models/marts
cat > models/marts/_marts.yml <<'CLAUDE_DE_EOF'
version: 2

models:
  - name: fct_orders
    description: One row per order with the total paid.
    columns:
      - name: order_id
        data_tests: [unique, not_null]

  - name: dim_customers
    description: One row per customer with order count and lifetime value (returned orders excluded).
    columns:
      - name: customer_id
        data_tests: [unique, not_null]
CLAUDE_DE_EOF
dbt test   # expected to fail

# ---------- step 6: Look at the duplicate raw rows ----------
duckdb shop.duckdb -c "SELECT id, first_name, last_name, country, updated_at FROM raw_customers WHERE id = 3"

# ---------- step 7: Keep the latest row per customer, then dbt build ----------
mkdir -p models/staging
cat > models/staging/stg_customers.sql <<'CLAUDE_DE_EOF'
select
    id                            as customer_id,
    first_name,
    last_name,
    lower(email)                  as email,
    country,
    cast(updated_at as timestamp) as updated_at
from {{ ref('raw_customers') }}
-- raw_customers can hold the same customer more than once: keep the latest row
qualify row_number() over (partition by id order by updated_at desc) = 1
CLAUDE_DE_EOF
dbt build

# ---------- step 8: Check the customer mart again ----------
duckdb shop.duckdb -c "SELECT customer_id, first_name, last_name, country, orders, lifetime_value FROM dim_customers ORDER BY lifetime_value DESC LIMIT 5"

# ---------- step 9: Generate the docs and write the project rules into CLAUDE.md ----------
cat > CLAUDE.md <<'CLAUDE_DE_EOF'
# shop-dbt

dbt project for the online shop. Adapter: dbt-duckdb, database file `shop.duckdb` (see profiles.yml).

- Raw data: seeds/ (raw_customers, raw_orders, raw_payments)
- Layers: models/staging (stg_*), models/marts (fct_*, dim_*)

## Commands
- `dbt build` runs seeds, models and tests in dependency order
- `dbt build --select stg_customers+` one model and everything downstream
- `dbt docs generate` after changing descriptions

## Rules
- Every model gets unique + not_null tests on its primary key
- Staging models: rename, cast, dedupe. No joins, no business logic
- Fix bad raw data in staging, not in each mart
- Run `dbt build` and make sure it passes before saying a change is done
CLAUDE_DE_EOF
dbt docs generate
Next in this series

Part 6: Connect Claude to Your Database with MCP

The project and its data are invented for teaching. Every file on this page is the real file from the demo project, and every output is the real output of the command above it, captured when the video was made (each step ran twice in a fresh folder and the outputs were compared). The Claude screens in the video are a simplified recreation for teaching. Always review what Claude changes and runs. Claude plans, features and limits are current as of November 2026; check the official Claude and Claude Code documentation before you rely on them. Part of the Claude for Data Engineers series on TechBrothersIT.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.