How to Use Diesel ORM in Rust

Getting Started

Install Diesel ORM by adding it to Cargo.toml, installing the CLI, and running setup commands to generate your database schema.

Install Diesel by adding it to your Cargo.toml dependencies and running diesel CLI commands to set up your database schema.

[dependencies]
diesel = "2.2.10"

[dev-dependencies]
diesel = { version = "2.2.10", features = ["sqlite", "libsqlite3-sys/bundled"] }
  1. Add the diesel crate to your Cargo.toml with the sqlite and libsqlite3-sys/bundled features for embedded testing.
  2. Run cargo install diesel_cli --no-default-features --features sqlite to install the command-line tool.
  3. Execute diesel setup to create the database and run migrations.
  4. Generate your schema file with diesel print-schema > src/schema.rs.
  5. Define your database models in src/lib.rs using the diesel::prelude macros.