gpt4 book ai didi

database - 返回在PostgreSQL中使用Rocket和Diesel创建的单个记录(Rust)

转载 作者:行者123 更新时间:2023-12-03 11:43:27 30 4
gpt4 key购买 nike

我正在尝试基于以下教程构建一个简单的Web应用程序:onetwothree。它们都使用略有不同的方法,并且是针对所用库的旧版本编写的,因此当前的Diesel和Rocket API略有不同。我想要响应POST请求返回创建的记录或记录ID。在这一点上,我只能返回记录列表-单个记录的列表(尝试根据Vec.first()不编译返回单个记录)。

Cargo.toml :

[dependencies]
# chrono is needed with serialization/deserialisation options
chrono = { version = "0.4.10", features = ["serde"] }
diesel = { version = "1.4.3", features = ["postgres", "chrono", "network-address"] }
ipnetwork = "0.15.1"
dotenv = "0.9.0"

# Lazy static initialization
lazy_static = "1.3.0"

# Rocket Webserver
rocket = "0.4.2"
rocket_contrib = {version = "0.4.2", features = ["json", "diesel_postgres_pool"]}

# Connection pooling
r2d2 = "0.8.7"
r2d2-diesel = "1.0.0"

# Serialization/Deserialization
serde = "1.0.104"
serde_json = "1.0.44"
serde_derive = "1.0.104"

models.rs :
#![allow(proc_macro_derive_resolution_fallback)]
use crate::schema::bs_date_forecast;

use chrono::{NaiveDate, NaiveDateTime};
use ipnetwork::IpNetwork;
use diesel::prelude::*;

#[derive(Queryable, Insertable, Serialize, Deserialize)]
#[table_name="bs_date_forecast"]
pub struct BsDateForecast {
pub id: Option<i32>,
pub predicted_date: NaiveDate,
pub seer_ip: IpNetwork,
pub created_timestamp: NaiveDateTime, // Defined automatically at the database level
}

impl BsDateForecast {

// Would like to return the record (BsDateForecast) instead, but don't know how
pub fn create(forecast: BsDateForecast,
conn: &PgConnection) -> QueryResult<Vec<BsDateForecast>> {
diesel::insert_into(bs_date_forecast::table)
.values(&forecast)
.get_results(conn)
}

}

schema.rs :
table! {
bs_date_forecast (id) {
id -> Nullable<Int4>,
predicted_date -> Date,
seer_ip -> Inet,
created_timestamp -> Timestamptz,
}
}

routes.rs :
use diesel::{self, prelude::*};

use rocket_contrib::json::{Json, JsonValue};
use rocket::response::status;

use crate::models::{BsDateForecast};
use crate::schema;
use crate::DbConn;
use serde_json::Value;



#[post("/predictions", format = "application/json", data = "<prediction>")]
pub fn create(prediction: Json<BsDateForecast>, conn: DbConn) -> Json<Vec<BsDateForecast>> {
let insert = BsDateForecast{id: None, ..prediction.into_inner()};
Json(BsDateForecast::create(insert, &conn))
}

最佳答案

这就像更改从数据库加载值的函数一样简单。

impl BsDateForecast {

// Would like to return the record (BsDateForecast) instead, but don't know how
pub fn create(forecast: BsDateForecast,
conn: &PgConnection) -> QueryResult<BsDateForecast> {
diesel::insert_into(bs_date_forecast::table)
.values(&forecast)
.get_result(conn)
}

}

有关详细信息(以及其他可用方法),请参见 RunQueryDsl 的文档。

关于database - 返回在PostgreSQL中使用Rocket和Diesel创建的单个记录(Rust),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60026192/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com