← 返回命令列表

Linux command

diesel 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Setup database

diesel setup

Generate a new migration

diesel migration generate [migration_name]

Run pending migrations

diesel migration run

Revert last migration

diesel migration revert

Print current schema

diesel print-schema

Generate schema.rs from database

diesel print-schema > src/schema.rs

Check migration status

diesel migration list

说明

Diesel CLI is the command-line tool for the Diesel ORM, a safe, extensible ORM and query builder for Rust. It manages database migrations and generates Rust code representing the database schema. Migrations are SQL files in a migrations directory, with "up" and "down" files for applying and reverting changes. The CLI tracks applied migrations in a database table. The print-schema command introspects the database and generates Rust code (schema.rs) that Diesel uses for compile-time query validation. This ensures queries are type-checked against the actual database structure.

参数

setup
Create database and run migrations.
migration generate _name_
Create new migration files.
migration run
Run all pending migrations.
migration revert
Revert the latest migration.
migration redo
Revert and re-run latest migration.
migration list
List all migrations and their status.
print-schema
Output database schema as Rust code.
database setup
Create the database.
database reset
Drop and recreate database.
--database-url _url_
Database connection URL.
--config-file _file_
Path to diesel.toml.
--migration-dir _dir_
Directory containing migrations.

FAQ

What is the diesel command used for?

Diesel CLI is the command-line tool for the Diesel ORM, a safe, extensible ORM and query builder for Rust. It manages database migrations and generates Rust code representing the database schema. Migrations are SQL files in a migrations directory, with "up" and "down" files for applying and reverting changes. The CLI tracks applied migrations in a database table. The print-schema command introspects the database and generates Rust code (schema.rs) that Diesel uses for compile-time query validation. This ensures queries are type-checked against the actual database structure.

How do I run a basic diesel example?

Run `diesel setup` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does setup do in diesel?

Create database and run migrations.