作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用Tokio-Diesel在我的ORM中使用async
/await
。我无法使用Diesel的belonging_to
关联,因为它提示我需要静态生命周期。我已经尝试了异步块,但没有改变生命周期错误。
Cargo.toml
[package]
name = "tokio_diesel"
version = "0.1.0"
authors = ["servonlewis"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diesel = { version = "1.4.3", features = ["postgres", "r2d2", "serde_json", "chrono", "uuid"] }
r2d2 = "0.8.8"
tokio = { version = "0.2", features = ["full"] }
tokio-diesel = "0.3.0"
uuid = { version = "0.6.5", features = ["serde", "v4"] }
main.rs
#[macro_use]
extern crate diesel;
use diesel::prelude::*;
use tokio_diesel::*;
table! {
articles (id) {
id -> Integer,
title -> Text,
}
}
table! {
articles_visible_to_groups (id) {
id -> Integer,
article_id -> Integer,
groups_id -> Integer,
}
}
table! {
groups (id) {
id -> Integer,
name -> Varchar,
}
}
joinable!(articles_visible_to_groups -> articles (article_id));
joinable!(articles_visible_to_groups -> groups (groups_id));
allow_tables_to_appear_in_same_query!(articles, articles_visible_to_groups, groups,);
use crate::articles::dsl::*;
use crate::groups::dsl::*;
use diesel::pg::PgConnection;
use diesel::r2d2::{ConnectionManager, Pool};
pub type PostgresPool = Pool<ConnectionManager<PgConnection>>;
pub fn get_pool() -> PostgresPool {
let url = "postgres://actix:actix@localhost:5432/actix".to_string();
let mgr = ConnectionManager::<PgConnection>::new(url);
r2d2::Pool::builder()
.build(mgr)
.expect("could not build connection pool")
}
#[derive(Queryable, Identifiable, Associations, Debug)]
#[table_name = "articles"]
pub struct Article {
pub id: i32,
pub title: String,
}
#[derive(Identifiable, Queryable, Associations, Debug, PartialEq)]
#[belongs_to(Group, foreign_key = "groups_id")]
#[belongs_to(Article)]
#[table_name = "articles_visible_to_groups"]
pub struct ArticleVisibleToGroup {
pub id: i32,
pub article_id: i32,
pub groups_id: i32,
}
#[derive(Queryable, Identifiable, Associations, Debug, PartialEq)]
pub struct Group {
pub id: i32,
pub name: String,
}
pub async fn groups_who_can_see(conn: &PostgresPool, an_id: i32) -> Vec<Group> {
use diesel::pg::expression::dsl::any;
let record = articles
.find(an_id)
.load_async::<Article>(conn)
.await
.unwrap();
let list = ArticleVisibleToGroup::belonging_to(&record)
.select(articles_visible_to_groups::groups_id)
.load_async::<i32>(conn)
.await
.unwrap();
groups
.filter(groups::id.eq_all(any(list)))
.load_async::<Group>(conn)
.await
.unwrap()
}
#[tokio::main]
async fn main() {
println!("Hello, world!");
// pool.get_ref().to_owned(),
let pool = get_pool();
let res = groups_who_can_see(&pool, 1).await;
println!("{:#?}", res);
}
error[E0597]: `record` does not live long enough
--> src/main.rs:80:52
|
80 | let list = ArticleVisibleToGroup::belonging_to(&record)
| ------------------------------------^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `record` is borrowed for `'static`
...
91 | }
| - `record` dropped here while still borrowed
最佳答案
看起来我能够通过不使用归属感到而是使用过滤器来解决此问题。
代替上面的示例,该示例可以异步工作。
pub async fn groups_who_can_see(
conn: &PostgresPool,
an_id: String,
) -> FieldResult<Vec<Group>> {
use diesel::pg::expression::dsl::any;
let my_id = uuid::Uuid::parse_str(&an_id)?;
let list = articles_visible_to_groups
.filter(article_id.eq_all(my_id))
.select(articles_visible_to_groups::groups_id)
.load_async::<uuid::Uuid>(&conn)
.await?;
let data = groups
.filter(groups::id.eq_all(any(list)))
.load_async::<Group>(&conn)
.await;
graphql_translate(data)
}
关于rust - “argument requires that `记录` is borrowed for ` 'static`”与tokio柴油一起使用归属关系时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63256779/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我是一名优秀的程序员,十分优秀!