【问题标题】:Following "getting started" diesel tutorial, but with sqlite, causes遵循“入门”柴油教程,但使用 sqlite,原因
【发布时间】:2021-08-21 05:56:51
【问题描述】:

我正在尝试关注:https://diesel.rs/guides/getting-started 但我正在使用:

echo DATABASE_URL=/tmp/diesel_demo.sqlite > .env

而不是 Postgres 数据库。

我已将所有出现的Pg 更改为Sqlite,并将SERIAL 更改为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?

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 = establish_connection();
    let results = posts.filter(published.eq(true))
        .limit(5)
        .load::<Post>(&connection)
        .expect("Error loading posts");

    println!("Displaying {} posts", results.len());
    for post in results {
        println!("{}", post.title);
        println!("----------\n");
        println!("{}", post.body);
    }
}

up.sql:

CREATE TABLE posts (
  id INTEGER PRIMARY KEY,
  title VARCHAR NOT NULL,
  body TEXT NOT NULL,
  published BOOLEAN NOT NULL DEFAULT 'f'
)

models.rs(自动生成):

#[derive(Queryable)]
pub struct Post {
    pub id: i32,
    pub title: String,
    pub body: String,
    pub published: bool,
}

我不明白为什么 Diesel 期望 idNullable

【问题讨论】:

  • Diesel 期望 idNullable&lt;Integer&gt;,因为 sqlite 确实通过 PRAGMA TABLE_INFO('…') 表明该列可以为空。您可以在 schema.rs 文件中查看推断的类型。

标签: sqlite rust rust-diesel


【解决方案1】:

NOT NULL 添加到up.sql 中的id 字段修复了它。

【讨论】:

    猜你喜欢
    • 2021-10-07
    • 2023-03-27
    • 2014-04-04
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2020-04-14
    • 2015-10-22
    • 2021-03-17
    相关资源
    最近更新 更多