遵循“入门”柴油教程,但使用 sqlite,导致

时间:2021-06-03 05:34:15

标签: sqlite rust rust-diesel

我正在尝试关注: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

1 个答案:

答案 0 :(得分:0)

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