Diesel
Overview
Diesel is a safe, extensible ORM and query builder for Rust that uses Rust's type system to catch query errors at compile time and eliminate boilerplate. It supports PostgreSQL, MySQL, and SQLite with a zero-cost, Rust-like query DSL. Read this when building Rust applications that need type-safe, performant database access.
Official sources
Install / setup
cargo install diesel_cli --no-default-features --features postgres
Add to Cargo.toml:
diesel = { version = "2.2.0", features = ["postgres"] }
dotenvy = "0.15"
Core concepts
- diesel_cli: command-line tool that creates the database, manages migrations, and generates schema (
diesel setup, diesel migration).
- schema.rs: generated
table! definitions describing tables and columns, used for compile-time query checking.
- Migrations: pairs of
up.sql/down.sql files applied and reverted by the CLI.
- Connection: a typed connection (e.g.
PgConnection) established with a database URL.
- Query DSL: methods like
filter, select, load, insert_into compile to checked SQL.
- Derive macros:
Queryable, Insertable, Selectable map Rust structs to rows.
Best practices
- Install
diesel_cli with only the features you need (e.g. --no-default-features --features postgres) to avoid pulling in unused client libraries (per the getting-started guide).
- Keep
schema.rs generated from migrations rather than hand-editing it, so it matches the database.
- Store the connection URL in a
.env file (used with dotenvy) as shown in the guide.
- Write reversible migrations (
up.sql + down.sql) and run them through the CLI.
Common pitfalls
- Installing
diesel_cli without the right backend feature → build errors or a CLI that cannot talk to your database; specify the backend feature.
- Editing
schema.rs by hand → drift from the real schema; regenerate it from migrations instead.
Examples
use diesel::prelude::*;
#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::schema::users)]
struct User { id: i32, name: String }
let conn = &mut PgConnection::establish(&database_url)?;
let results = users::table
.filter(users::name.eq("Ada"))
.select(User::as_select())
.load(conn)?;
Further reading
Related skills
- ../gorm — Go's standard ORM (contrast: another typed-language ORM)
- ../prisma — type-safe ORM in the TS ecosystem
1---2name: diesel3description: Safe, extensible compile-time-checked ORM and query builder for Rust; consult for type-safe SQL against PostgreSQL, MySQL, or SQLite in Rust.4domain: stack5category: orm6tags: [orm, rust, query-builder, postgresql, sqlite, mysql]7official_sources:8 - https://diesel.rs/guides/9 - https://github.com/diesel-rs/diesel10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/diesel13---1415# Diesel1617## Overview18Diesel is a safe, extensible ORM and query builder for Rust that uses Rust's type system to catch query errors at compile time and eliminate boilerplate. It supports PostgreSQL, MySQL, and SQLite with a zero-cost, Rust-like query DSL. Read this when building Rust applications that need type-safe, performant database access.1920## Official sources21- Docs / guides: https://diesel.rs/guides/22- Repo: https://github.com/diesel-rs/diesel23- Install / getting started: https://diesel.rs/guides/getting-started2425## Install / setup26```bash27cargo install diesel_cli --no-default-features --features postgres28```29Add to `Cargo.toml`:30```toml31diesel = { version = "2.2.0", features = ["postgres"] }32dotenvy = "0.15"33```3435## Core concepts36- **diesel_cli**: command-line tool that creates the database, manages migrations, and generates schema (`diesel setup`, `diesel migration`).37- **schema.rs**: generated `table!` definitions describing tables and columns, used for compile-time query checking.38- **Migrations**: pairs of `up.sql`/`down.sql` files applied and reverted by the CLI.39- **Connection**: a typed connection (e.g. `PgConnection`) established with a database URL.40- **Query DSL**: methods like `filter`, `select`, `load`, `insert_into` compile to checked SQL.41- **Derive macros**: `Queryable`, `Insertable`, `Selectable` map Rust structs to rows.4243## Best practices44- Install `diesel_cli` with only the features you need (e.g. `--no-default-features --features postgres`) to avoid pulling in unused client libraries (per the getting-started guide).45- Keep `schema.rs` generated from migrations rather than hand-editing it, so it matches the database.46- Store the connection URL in a `.env` file (used with `dotenvy`) as shown in the guide.47- Write reversible migrations (`up.sql` + `down.sql`) and run them through the CLI.4849## Common pitfalls50- Installing `diesel_cli` without the right backend feature → build errors or a CLI that cannot talk to your database; specify the backend feature.51- Editing `schema.rs` by hand → drift from the real schema; regenerate it from migrations instead.5253## Examples54```rust55use diesel::prelude::*;5657#[derive(Queryable, Selectable)]58#[diesel(table_name = crate::schema::users)]59struct User { id: i32, name: String }6061let conn = &mut PgConnection::establish(&database_url)?;62let results = users::table63 .filter(users::name.eq("Ada"))64 .select(User::as_select())65 .load(conn)?;66```6768## Further reading69- https://diesel.rs/guides/getting-started — getting started70- https://diesel.rs/guides/all-about-inserts — inserts guide7172## Related skills73- ../gorm — Go's standard ORM (contrast: another typed-language ORM)74- ../prisma — type-safe ORM in the TS ecosystem