在当前范围内没有为 struct `schema::users::columns::roles` 找到名为 `contains` 的方法

Posted

技术标签:

【中文标题】在当前范围内没有为 struct `schema::users::columns::roles` 找到名为 `contains` 的方法【英文标题】:no method named `contains` found for struct `schema::users::columns::roles` in the current scope 【发布时间】:2021-05-13 00:44:27 【问题描述】:

我是 rust 新手,正在尝试创建示例 webapi。我正在使用 postgres 并有一个名为“roles”的列作为一种数组。我想使用包含来自diesel::expression_methods::PgArrayExpressionMethods 的方法,但出现错误提示“在当前范围内没有为结构schema::users::columns::roles 找到名为contains 的方法”

schema.rs

table! 
    users (id) 
        id -> Uuid,
        username -> Varchar,
        email -> Varchar,
        password -> Varchar,
        first_name -> Nullable<Varchar>,
        last_name -> Nullable<Varchar>,
        active -> Bool,
        roles -> Nullable<Array<Text>>,
        created_at -> Timestamp,
        updated_at -> Timestamp,
    

在处理方法中:

users.filter(roles.contains(vec!["admin"])).get_results::<User>(&conn)

【问题讨论】:

【参考方案1】:

就在我写这个问题的时候,我发现了问题。它不适用于 nullable 数组列。我已经使用默认的空数组重新启动了迁移,现在它可以工作了:)

新架构.rs

table! 
    users (id) 
        id -> Uuid,
        username -> Varchar,
        email -> Varchar,
        password -> Varchar,
        first_name -> Nullable<Varchar>,
        last_name -> Nullable<Varchar>,
        active -> Bool,
        roles -> Array<Text>,
        created_at -> Timestamp,
        updated_at -> Timestamp,
    

新迁移/.../up.sql

.
.
  roles text [] not null default array[]::text[], 
.
.


【讨论】:

以上是关于在当前范围内没有为 struct `schema::users::columns::roles` 找到名为 `contains` 的方法的主要内容,如果未能解决你的问题,请参考以下文章