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

fad*_*bee 6 sqlite rust rust-diesel

我试图遵循: 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 = 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

上.sql:

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

models.rs(自动生成):

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

我不明白为什么迪塞尔会id这样期望Nullable

fad*_*bee 10

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