【发布时间】:2021-11-07 09:36:51
【问题描述】:
我想使用 PostgreSQL 13 对 diesel 进行 IN 查询,以从某些 id 集合中的歌曲表中选择歌曲集合。数据库执行的 SQL 可能如下所示:
select * from songs where id in(1,2,3)
我试图用这样的生锈柴油方式做的事情:
pub fn songs(ids: Vec<i64>){
use schema::songs::dsl::*;
let connection = config::establish_connection();
let results = songs.filter(id.contains(ids))
.load::<QuerySongs>(&connection)
.expect("Error loading posts");
}
似乎没有用,当我使用cargo build 编译项目代码时显示如下错误:
error[E0599]: the method `contains` exists for struct `songs::schema::songs::columns::id`, but its trait bounds were not satisfied
--> src/biz/music/songs.rs:37:35
|
37 | let results = songs.filter(id.contains(ids))
| ^^^^^^^^ method cannot be called on `songs::schema::songs::columns::id` due to unsatisfied trait bounds
我阅读了docs 和source code demo,但它太短了,并没有提到如何在集合中按 id 执行查询。我应该怎么做才能在锈柴油(diesel = { version = "1.4.4", features = ["postgres"] })中进行 IN 查询?这是一个最小可重复的demo。
【问题讨论】:
标签: postgresql rust rust-diesel