SQLFluff dbt Templater

4.1.0 · active · verified Fri Apr 10

SQLFluff-templater-dbt is a plugin for SQLFluff, the SQL linter for humans, specifically designed to correctly parse and compile SQL projects using dbt (data build tool). It extends SQLFluff's capabilities to lint dbt models by leveraging dbt's templating engine. The project releases in conjunction with the main SQLFluff library, with version 4.1.0 being the latest as of March 2026. [2, 5, 13]

Warnings

Install

Quickstart

To use sqlfluff-templater-dbt, you must first install the package along with `sqlfluff`, `dbt-core`, and your specific `dbt` adapter. You then configure SQLFluff in a `.sqlfluff` file at your dbt project root, explicitly setting `templater = dbt` and specifying your SQL `dialect`. [1, 4, 12] It's also recommended to configure `project_dir` and `profiles_dir` within the `[sqlfluff:templater:dbt]` section and enable dbt built-ins in the `[sqlfluff:templater:jinja]` section. [1, 4, 9] An `.sqlfluffignore` file is crucial to prevent linting dbt compilation artifacts. [1, 12] Once configured, you can run `sqlfluff lint` or `sqlfluff fix` from your project's root.

# 1. Create a dbt project, e.g., using `dbt init my_dbt_project`
# 2. Navigate into your dbt project directory
# 3. Create a .sqlfluff configuration file:
# .sqlfluff
# ---
# [sqlfluff]
# dialect = snowflake # Or your dbt adapter's dialect
# templater = dbt
#
# [sqlfluff:templater:dbt]
# project_dir = ./
# profiles_dir = ~/.dbt/ # Or path to your dbt profiles.yml
#
# [sqlfluff:templater:jinja]
# apply_dbt_builtins = True # Enable dbt macros like `ref`, `var`, `is_incremental()`
# ---

# 4. Create an optional .sqlfluffignore file to exclude dbt artifacts:
# .sqlfluffignore
# ---
# target/
# dbt_packages/
# macros/
# venv/
# .venv/
# node_modules/
# logs/
# ---

# 5. Run SQLFluff to lint your dbt project
# (Make sure your dbt profile is configured for compilation if models query database at compile time)
import subprocess
import os

# Example dbt model file
with open('models/my_model.sql', 'w') as f:
    f.write("-- my_model.sql\n\nSELECT {{ ref('another_model') }} FROM {{ source('my_schema', 'my_table') }}\n")

# Lint all SQL files in the project
print("\n--- Running sqlfluff lint ---")
result_lint = subprocess.run(['sqlfluff', 'lint', '.'], capture_output=True, text=True)
print(result_lint.stdout)
if result_lint.stderr: print("Error:", result_lint.stderr)

# Fix linting issues (if any are auto-fixable)
print("\n--- Running sqlfluff fix ---")
result_fix = subprocess.run(['sqlfluff', 'fix', '.'], capture_output=True, text=True)
print(result_fix.stdout)
if result_fix.stderr: print("Error:", result_fix.stderr)

view raw JSON →