1

我正在struct GuildId(i64);为我的柴油模型结构中的列使用新类型。目前我正在实现这些特征:

#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
pub struct $name(pub i64);

impl AsExpression<BigInt> for $name { /* delegate to <i64 as AsExpression<BigInt> */ */ }

impl<ST, DB: Backend> Queryable<ST, DB> for $name
where i64: FromSql<ST, DB> { /* also delegate to i64 */

但是,当我尝试在以下模型结构中使用此类型时:

#[derive(Associations, Identifiable, Queryable)]
#[belongs_to(Guild)]
struct Channel {
    guild_id: GuildId,
    // other fields
}

#[derive(Identifiable, Queryable)]
struct Guild {
    id: GuildId,
    // other fields
}

Channel仍然没有执行BelongingToDsl。当我尝试将其转换为特征时,它无法编译并显示以下消息:

error[E0277]: the trait bound `diesel::query_builder::select_statement::SelectStatement<webcord_schema::schema::channels::table>: diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Eq<webcord_schema::schema::channels::columns::guild_id, &webcord_schema::models::GuildId>>` is not satisfied
  --> src/index/guild.rs:23:32
   |
23 |                 let channels = <models::Channel as BelongingToDsl<&models::Guild>>::belonging_to(&guild)
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Eq<webcord_schema::schema::channels::columns::guild_id, &webcord_schema::models::GuildId>>` is not implemented for `diesel::query_builder::select_statement::SelectStatement<webcord_schema::schema::channels::table>`
   |
   = help: the following implementations were found:
             <diesel::query_builder::select_statement::SelectStatement<F, S, D, W, O, L, Of, G, LC> as diesel::query_dsl::filter_dsl::FilterDsl<Predicate>>
   = note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Eq<webcord_schema::schema::channels::columns::guild_id, &webcord_schema::models::GuildId>>` for `webcord_schema::schema::channels::table`

error[E0277]: the trait bound `webcord_schema::models::GuildId: diesel::expression::Expression` is not satisfied
  --> src/index/guild.rs:23:32
   |
23 |                 let channels = <models::Channel as BelongingToDsl<&models::Guild>>::belonging_to(&guild)
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::expression::Expression` is not implemented for `webcord_schema::models::GuildId`
   |
   = note: required because of the requirements on the impl of `diesel::expression::Expression` for `&webcord_schema::models::GuildId`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::BigInt>` for `&webcord_schema::models::GuildId`
   = note: required because of the requirements on the impl of `diesel::query_dsl::belonging_to_dsl::BelongingToDsl<&webcord_schema::models::Guild>` for `webcord_schema::models::Channel`

我缺少什么特质?

4

1 回答 1

2

该错误与 无关BelongingToDsl,而是与自定义新类型包装器的不完整实现有关。

由于错误消息表明您缺少新类型包装器的 trait impl:

error[E0277]: the trait bound `webcord_schema::models::GuildId: diesel::expression::Expression` is not satisfied
  --> src/index/guild.rs:23:32
   |
23 |                 let channels = <models::Channel as BelongingToDsl<&models::Guild>>::belonging_to(&guild)
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::expression::Expression` is not implemented for `webcord_schema::models::GuildId`
   |
   = note: required because of the requirements on the impl of `diesel::expression::Expression` for `&webcord_schema::models::GuildId`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::BigInt>` for `&webcord_schema::models::GuildId`
   = note: required because of the requirements on the impl of `diesel::query_dsl::belonging_to_dsl::BelongingToDsl<&webcord_schema::models::Guild>` for `webcord_schema::models::Channel`

有趣的行是 中的第二行= note: required because of the requirements on the impl of diesel::expression::AsExpression<diesel::sql_types::BigInt> for '&webcord_schema::models::GuildId。这意味着您至少需要添加一个AsExpression<_>impl 来引用您的新类型包装器。

所以现在一般来说:有这个测试用例展示了如何实现自定义类型。您将看到 rust 端的自定义类型使用两个自定义派生 (AsExpressionFromSqlRow),它们基本上实现了您已经手动实现的特征以及缺少的特征。此外,需要ToSql/ FromSqlimpl 来描述应如何将类型转换为/从 sql 类型转换。

总结一下您的类型定义应该如下所示:

#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, AsExpression, FromSqlRow)]
#[sql_type = "diesel::sql_types::BigInt")]
pub struct $name(pub i64);

impl<DB> ToSql<diesel::sql_types::BigInt, DB> for $name 
where DB: Backend,
    i64: ToSql<diesel::sql_types::BigInt, DB>,
{
    fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
        <i64 as ToSql<diesel::sql_types::BigInt, DB>>::to_sql(&self.0, out)
    }
}

impl<DB> FromSql<diesel::sql_types::BigInt, DB> for $name 
where DB: Backend,
    i64: FromSql<diesel::sql_types::BigInt, DB>
{
    fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
        <i64 as FromSql<diesel::sql_types::BigInt, DB>>::from_sql(bytes).map($name)
    }
}
于 2020-01-28T11:48:53.700 回答