如何制作柴油汽车自动生成模型。

10

我现在使用这个命令在 Rust Diesel 中生成模式:

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
}

现在我使用handle方式编写实体。我该如何让它通过Diesel CLI生成?我已经阅读了文档,但没有找到任何有用的配置。


你的意思是从数据库表定义中生成 Music 模型吗? - Njuguna Mureithi
是的,这就是我的意思,通过手动编写模型很无聊。@NjugunaMureithi - Dolphin
1个回答

15

您正在寻找diesel_cli_ext

首先安装diesel_cli_ext:

cargo install diesel_cli_ext

如果您还没有按照diesel的方式生成模式文件,则需要生成模式文件:

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
}

1
添加 -t 以生成带有表名的模型,diesel_ext --model -t> src/models.rs - Leslie
1
有没有可能以相反的方式进行,让 Rust 中的模型升级数据库? - Oliver Dixon
@OliverDixon 您需要为您的 Rust 代码编写解析器。您可能正在寻找类似于 Prisma 的东西,它将管理模式,然后您可以将其与 Diesel 结合使用以生成 Rust 代码。 - Njuguna Mureithi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接