Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to make diesel auto generate model

I am now using this command to generate schema in rust diesel:

diesel --database-url postgres://postgres:kZLxttcZSN@127.0.0.1:5432/rhythm \
migration run --config-file="${CURRENT_DIR}"/diesel-rhythm.toml

and this is the 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 model entity? the entity may look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

now I write the entity by handle. what should I do to make it generate by diesel cli, I read the document and did not found any useful configuration about this.

>Solution :

You are looking for diesel_cli_ext

First install diesel_cli_ext:

cargo install diesel_cli_ext

Then you would have to do:

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading