Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Diesel auto-generate models?

I am using this command to generate schema for Diesel:

diesel --database-url postgres://postgres:[email protected]:5432/rhythm \
migration run --config-file="${CURRENT_DIR}"/diesel-rhythm.toml

and this is the diesel.toml config:

[print_schema]
file = "src/model/diesel/rhythm/rhythm_schema.rs"

# This will cause only the users and posts tables to be output
filter = { only_tables = ["favorites", "songs", "playlist"] }

Is it possible to make Diesel auto-generate the models? The model may look like this:

#[derive(Serialize, Queryable, Deserialize, Default)]
pub struct Music {
    pub id: i64,
    pub name: String,
    pub source_id: String
}

Right now I write the models by hand. What should I do to make it generate from the Diesel CLI? I read through the documentation but did not found any useful configuration about this.

like image 819
Dolphin Avatar asked Dec 03 '25 17:12

Dolphin


1 Answers

You are looking for diesel_cli_ext

First install diesel_cli_ext:

cargo install diesel_cli_ext

[Then] you would have to generate your schema file the diesel way if you haven't yet:

diesel print-schema > src/schema.rs

Finally you have to generate the models file:

diesel_ext --model > src/models.rs

The models in your schema file would be generated in src/models.rs eg:

#[derive(Queryable)]
pub struct Music {
    pub id: i64,
    pub name: String,
    pub source_id: String
}
like image 118
Njuguna Mureithi Avatar answered Dec 06 '25 09:12

Njuguna Mureithi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!