我正在尝试使用Diesel和 PostgreSQL 执行插入多个列。
这是添加新的插入功能Project
-
pub fn insert(project: NewProject, program_id: i32, conn: &PgConnection) -> bool {
use schema::projects::dsl::*;
use schema::projects::dsl::{title as t};
use schema::projects::dsl::{program_id as prog_id};
let NewProject {
title
} = project;
diesel::insert_into(projects)
.values((t.eq(title), prog_id.eq(program_id)))
.execute(conn)
.is_ok()
}
并且Project
和NewProject
#[derive(Queryable, Serialize, Debug, Clone)]
pub struct Project {
pub id: i32,
pub title: String,
pub program_id: i32,
pub is_archived: bool
}
#[derive(Serialize, Deserialize, Insertable)]
#[table_name = "projects"]
pub struct NewProject {
pub title: String
}
项目表如下所示 -
CREATE TABLE projects (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
program_id INTEGER NOT NULL REFERENCES programs (id),
is_archived BOOLEAN NOT NULL DEFAULT FALSE
);
和 schema.rs -
table! {
projects (id) {
id -> Int4,
title -> Varchar,
program_id -> Int4,
is_archived -> Bool,
}
编译时我收到一条错误消息 -
标题 | ^^^^^ 预期结构
std::string::String
,找到结构schema::projects::columns::title
和
.execute(conn) | ^^^^^^^ 预期结构
diesel::query_source::Never
,找到结构diesel::query_source::Once
当我这样做时,我没有收到编译错误
.values(&project)
而是在插入函数中。