API Reference

Diesel

Safe, extensible ORM and query builder for Rust that checks SQL against the schema at compile time.

Official Documentation ↗
CompatibilityThis documentation covers version diesel 2.2 / PostgreSQL·MySQL·SQLite / Rust 1.70+. Check the official docs for the latest API surface.

Installation

cargocargo add diesel

Methods

Overview

Diesel is a type-safe ORM and query builder for Rust. Table and column types are known to the compiler, so invalid queries and type mismatches are caught before runtime.

Core Methods

Write typed queries with QueryDsl chains, insert with insert_into values, and execute against a connection. The diesel CLI manages migrations and generates the schema module.

Usage Notes

Keep the generated schema in sync via the CLI, use connection pooling in servers, and prefer typed query builders over raw SQL except for complex cases with sql_query and explicit mappings.

Common Pitfalls

A stale schema module causes type errors that seem unrelated. Borrowing the connection mutably conflicts with concurrent use, so pool connections. Raw SQL bypasses compile-time checks and needs careful mapping.

FAQ

How does Diesel check queries at compile time?
It derives schema types from a generated schema file and the table definitions. Queries that reference missing columns or mismatched types fail to compile.
What is diesel print-schema?
The diesel CLI generates a schema.rs from the live database, giving Rust types for tables and columns that the ORM uses to type-check queries.
How do I handle migrations?
Use diesel migration generate and diesel migration run. Migrations are SQL files tracked in order, and reviewing generated SQL is expected before applying in production.