【问题标题】:how to make diesel auto generate model如何制作柴油汽车生成模型
【发布时间】:2022-01-21 09:05:33
【问题描述】:

我现在正在使用此命令在 rust 柴油中生成架构:

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

这是 toml 配置:

[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"] }

是否可以让柴油汽车生成模型实体?实体可能如下所示:

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

现在我通过句柄编写实体。我应该怎么做才能让它由柴油 cli 生成,我阅读了文档并没有找到任何有用的配置。

【问题讨论】:

  • 您的意思是从 db.table 定义中生成Music 模型吗?
  • 是的,这就是我的意思,按句柄写模型很无聊。 @NjugunaMureithi

标签: rust rust-diesel


【解决方案1】:

您正在寻找diesel_cli_ext

首先安装diesel_cli_ext:

cargo install diesel_cli_ext

那么你必须这样做:

diesel print-schema > src/schema.rs

最后你要生成模型文件:

diesel_ext --model > src/models.rs

架构文件中的模型将在src/models.rs 中生成,例如:

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

【讨论】:

    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 2011-05-01
    • 2021-09-26
    • 2014-03-15
    • 2020-10-26
    • 1970-01-01
    • 2022-12-10
    • 2021-07-25
    相关资源
    最近更新 更多