Reputation: 121
I'm new to rust and trying to create a sample webapi. I'm using postgres and have a column named "roles" as a type of array. I want to use contains method from diesel::expression_methods::PgArrayExpressionMethods but having an error saying "no method named contains
found for struct schema::users::columns::roles
in the current scope"
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,
}
}
in the handler method:
users.filter(roles.contains(vec!["admin"])).get_results::<User>(&conn)
Upvotes: 0
Views: 1387
Reputation: 121
And just as I was writing this question, I've found the problem. It doesn't work with nullable array columns. I've rerolled the migrations with a default empty array and now it works :)
new schema.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,
}
}
new migrations/.../up.sql
.
.
roles text [] not null default array[]::text[],
.
.
Upvotes: 1