【发布时间】: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 期望 id 是 Nullable。
【问题讨论】:
-
Diesel 期望
id为Nullable<Integer>,因为 sqlite 确实通过PRAGMA TABLE_INFO('…')表明该列可以为空。您可以在schema.rs文件中查看推断的类型。
标签: sqlite rust rust-diesel