SQLFluff dbt Templater
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
- gotcha The `dbt` templater is not the default for SQLFluff; the default is `jinja`. You must explicitly set `templater = dbt` in your `.sqlfluff` configuration file. This setting cannot be overridden in subdirectory configuration files. [1]
- breaking Version compatibility with `dbt-core` can be a breaking change. SQLFluff 4.x explicitly drops support for `dbt` versions 1.4 and older, and supports up to `dbt` 1.10. Newer `dbt-core` releases (e.g., 1.8) have historically introduced incompatibilities that required `sqlfluff` updates. [8, 17, 19] Additionally, `dbt` Fusion engine is not natively compatible with the `sqlfluff-templater-dbt`, which relies on `dbt-core`'s templater. [14, 15]
- gotcha Using the `dbt` templater is generally more complex and slower than the default `jinja` templater because it needs to compile the full dbt project. If your dbt models access a database at compile time, the templater will also require database access. [1, 21]
- gotcha SQLFluff 4.1.0 introduced a `max_parse_depth` configuration setting (default 255) to protect against resource exhaustion from deeply nested SQL. Extremely complex or deeply nested dbt models might hit this limit, leading to parsing errors. [5, 11]
- breaking SQLFluff 3.x dropped support for Python 3.7. The latest versions of `sqlfluff` and `sqlfluff-templater-dbt` require Python 3.9 or newer. [8, 6]
- gotcha By default, errors encountered during dbt compilation are ignored by the templater (`dbt_skip_compilation_error = True`). This can mask underlying issues in your dbt project that prevent full template rendering. [1]
Install
-
pip install sqlfluff sqlfluff-templater-dbt dbt-core dbt-snowflake # Replace dbt-snowflake with your adapter
Quickstart
# 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)