10

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:

#[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.

Dolphin
  • 29,069
  • 61
  • 260
  • 539

1 Answers1

14

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
}
Njuguna Mureithi
  • 3,506
  • 1
  • 21
  • 41
  • 1
    Add -t to generate with the table name, diesel_ext --model -t> src/models.rs – Leslie Oct 26 '22 at 16:20
  • 1
    Is it possible to this the opposite way so that models inside Rust upgrade the database? – Oliver Dixon Mar 13 '23 at 15:45
  • @OliverDixon You would have to write a parser for your rust code. You are possibly looking for something like prisma, which would manage the schema, then you could combine it with diesel to generate rust code. – Njuguna Mureithi Mar 17 '23 at 21:00