标签: rust-diesel

Rust / Diesel:如何查询具有uuid的postgres表并将其插入

我有Diesel生成的以下架构:

table! {
user (id) {
    id -> Uuid,
    name -> Text
}
Run Code Online (Sandbox Code Playgroud)

和相关的模型

use diesel::{
    self,
    Queryable,
    Insertable,
};
use diesel::prelude::*;
use diesel::sql_types::Uuid;
use super::schema::user;

#[derive(Queryable)]
pub struct User {
    pub id: Uuid,
    pub name: String,
}

impl User {

    pub fn get(id: i32, connection: &PgConnection) -> Vec<User> {
        user::table.load::<User>(connection).unwrap()
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试编译此错误时显示错误:

table! {
user (id) {
    id -> Uuid,
    name -> Text
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试插入,Expression则会收到类似的错误消息,提示未实现。

这可能是我的依赖项有问题还是我可能忘记将其添加到模型中?

[dependencies]
rocket = "0.4.0-rc.1"
serde = "1.0"
serde_derive = "1.0" …
Run Code Online (Sandbox Code Playgroud)

rust rust-diesel

8
推荐指数
2
解决办法
1392
查看次数

Diesel 支持 Postgres `point` 类型吗?

我想使用Postgres 类型。我在柴油机类型列表中没有看到此类型列出。

编写包含point列的模型的正确方法是什么?

rust rust-diesel

7
推荐指数
1
解决办法
751
查看次数

在 Diesel 中执行正确的联接

我有一个函数,它接受两个可选的过滤参数,如果提供了这些参数,则过滤表中的数据。为此,我创建了一个盒装查询。我想用这个盒装查询创建一个正确的连接。Diesel 的当前文档没有提到右连接,但它似乎更喜欢这种belonging_to方法。我已将我的代码简化为一个示例:

#[macro_use] extern crate diesel;

use diesel::prelude::*;

table! {
    groups (id) {
        id -> Int4,
        name -> Text,
    }
}

table! {
    user_groups (id) {
        id -> Int4,
        user_id -> Int4,
        group_id -> Int4,
    }
}

allow_tables_to_appear_in_same_query!(groups, user_groups);

#[derive(Debug, Queryable)]
struct Group {
    id: i32,
    name: String,
}

#[derive(Debug, Queryable, Associations)]
#[belongs_to(Group)]
struct UserGroup {
    id: i32,
    user_id: i32,
    group_id: i32,
}

fn filter(
    name: Option<&str>,
    user_id: Option<i32>,
    conn: &diesel::PgConnection,
) -> Result<Vec<(Group, Vec<UserGroup>)>, Box<dyn std::error::Error>> { …
Run Code Online (Sandbox Code Playgroud)

postgresql rust rust-diesel

7
推荐指数
1
解决办法
1253
查看次数

如何在 Diesel 中编写 GROUP BY 或 HAVING 子句?

我正在尝试将以下 SQL 查询转换为相应的 Rust Diesel 代码:

SELECT COUNT(*)
FROM BookStore
WHERE BookName IN ('Lord of the Rings', 'Hobbit')
GROUP BY StoreId
HAVING COUNT(DISTINCT BookName) = 2
Run Code Online (Sandbox Code Playgroud)

到目前为止我能够将其翻译为:

let bookNames = vec!["Lord of the Rings", "Hobbit"];

let subquery = bookStores::table
.select(count_star())
.filter(bookName.eq_any(bookNames));
Run Code Online (Sandbox Code Playgroud)

我相信这可以翻译为:

SELECT COUNT(*)
FROM BookStore
WHERE BookName IN ('Lord of the Rings', 'Hobbit')
Run Code Online (Sandbox Code Playgroud)

我无法找到GROUP BYHAVINGSQL 子句的任何 Diesel 等效项。Diesel 中是否存在这些条款?

group-by having rust rust-diesel

7
推荐指数
0
解决办法
1808
查看次数

Rust 柴油 orm 查询

我是生锈和柴油奥姆的新手。我正在尝试在我的查询中执行以下操作:

  • 数数
  • 选择
  • 命令
  • 限制

但我收到错误。
我正在使用 postgres 数据库。

我已在评论中的查询上方添加了确切的错误。
这是我的代码:

模式.rs

table! {
    employee (employee_id) {
        employee_id -> Int4,
        name -> Nullable<Text>,
        age -> Nullable<Int4>,
        address -> Nullable<Text>,
        email -> Nullable<Text>,
        dept_id -> Int4,
        salary -> Nullable<Numeric>,
        created_on -> Nullable<Timestamp>,
        created_by -> Nullable<Text>,
        modified_on -> Nullable<Timestamp>,
        modified_by -> Nullable<Text>,
        is_active -> Nullable<Bool>,
    }
}
Run Code Online (Sandbox Code Playgroud)

模型.rs

#![allow(unused)]
#![allow(clippy::all)]
use super::schema::employee;

use bigdecimal::BigDecimal;
use chrono::NaiveDateTime;

#[derive(Queryable, Debug, Identifiable)]
#[table_name = "employee"]
#[primary_key(employee_id)]
pub struct Employee {
    pub employee_id: i32,
    pub name: …
Run Code Online (Sandbox Code Playgroud)

rust rust-crates rust-cargo rust-diesel

7
推荐指数
1
解决办法
6282
查看次数

如何使用 Diesel 和 SQLite 获取新创建值的 id?

DieselSqliteBackend没有实现该SupportsReturningClause特征,因此该get_result方法不能用于检索新创建的值。

还有其他方法可以找出插入行的 id 吗?Python对此有一个解决方案。到目前为止我找到的唯一解决方案是使用 UUID 作为 ids 而不是自动增量字段。

sqlite rust rust-diesel

7
推荐指数
1
解决办法
2966
查看次数

无法让Diesel推断SQLite表中可为空的Timestamp列

我正在尝试使用Diesel的推理功能来处理包含可空时间戳列的SQLite表。此GitHub项目的旧版本中提供了完整的示例。

books表定义如下:

create table books (
    id integer not null primary key,
    title varchar null,
    save_date timestamp null 
)
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下结构查询它:

pub mod domain {
    use chrono::naive;

    #[derive(Queryable)]
    pub struct Book {
        id : i32,
        title : Option<String>,
        save_date : Option<naive::NaiveDateTime>, 
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试实际执行查询时,如下所示:

fn main() {
    use self::schema::books::dsl::*;
    let database_url = env::var("DATABASE_URL").unwrap();
    let conn = sqlite::SqliteConnection::establish(&database_url).unwrap();
    let res = books.load::<domain::Book>(&conn);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

error[E0277]: the trait bound `std::option::Option<chrono::NaiveDateTime>: diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Timestamp>, _>` is not satisfied
  --> src/main.rs:32:21
   | …
Run Code Online (Sandbox Code Playgroud)

sqlite rust rust-diesel

6
推荐指数
0
解决办法
372
查看次数

如何使用diesel-rs选择色谱柱的子集?

我现在花了几个小时的时间来查询表的可用列的子集以及在其中包含计算。我知道这不是在选择查询中执行计算的最佳方式,但现在,我只是在开发一个原型,它应该是可行的。

diesel-rs在后端实现中将其用作所有数据库操作的 ORM。数据将存储在 PostgresSQL 服务器中。存储在数据库中的完整表是使用以下查询创建的:

CREATE TABLE airports
(
    id          SERIAL PRIMARY KEY,
    icao_code   VARCHAR(4) NOT NULL UNIQUE, -- the official ICAO code of the airport
    last_update TIMESTAMP  NOT NULL,        -- when were the information updated the last time?
    country     VARCHAR(2) NOT NULL,        -- two letter country code
    longitude   REAL       NOT NULL,        -- with 6 decimal places
    latitude    REAL       NOT NULL,        -- with 6 decimal places
    name        VARCHAR    NOT NULL         -- just a human readable name of the …
Run Code Online (Sandbox Code Playgroud)

postgresql rust rust-diesel

6
推荐指数
1
解决办法
5220
查看次数

Rust,Windows 10:Diesel 不起作用,Diesel 设置不提供输出

我是 Rust 新手,我想尝试使用 Diesel 和 Postgres。我已按照此处描述的步骤进行操作:

https://diesel.rs/guides/getting-started

但不幸的是,当它到来时diesel setup,什么也没有发生。我没有收到任何输出,没有错误消息,什么也没有。我已经使用命令安装了diesel_cli install diesel_cli --no-default-features --features postgres ,我在Windows 10上安装了Postgres 13。

这就是我的 .env 文件中的内容:DATABASE_URL=postgres://postgres:pwd@localhost:5432/diesel(pwd 是我的密码)。我创建了一个名为“diesel”的数据库,它使用默认设置在 Postgres 服务器上运行。目前它是空的,没有桌子。

我没有收到来自diesel setup或 的输出diesel migrate generate create_posts。甚至没有错误消息。

其他人也遇到过这个问题吗?知道什么会导致这种情况吗?

windows postgresql orm rust rust-diesel

6
推荐指数
1
解决办法
2170
查看次数

遵循“入门”柴油教程,但使用 sqlite,原因

我试图遵循: https: //diesel.rs/guides/getting-started但我正在使用:

echo DATABASE_URL=/tmp/diesel_demo.sqlite > .env
Run Code Online (Sandbox Code Playgroud)

而不是 Postgres 数据库。

我已更改所有出现的PgtoSqliteSERIALto INT,但出现以下错误:

error[E0277]: the trait bound `i32: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Integer>, Sqlite>` is not satisfied
  --> src/bin/show_posts.rs:14:10
   |
14 |         .load::<Post>(&connection)
   |          ^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Integer>, Sqlite>` is not implemented for `i32`    
How to get a result set where field value == row number?
Run Code Online (Sandbox Code Playgroud)

show_posts.rs:

extern crate diesel_demo;
extern crate diesel;

use self::diesel_demo::*;
use self::models::*;
use self::diesel::prelude::*;

fn main() {
    use diesel_demo::schema::posts::dsl::*;

    let connection = …
Run Code Online (Sandbox Code Playgroud)

sqlite rust rust-diesel

6
推荐指数
1
解决办法
3796
查看次数